java incorrect timezone

前端 未结 9 1006
说谎
说谎 2020-12-06 02:07

I have an instance of Java which seems to be using a completely incorrect time zone. Instead of using the Australia/Sydney time zone which Windows is using, it is using the

9条回答
  •  一生所求
    2020-12-06 02:44

    Try in your app to get default timezone, or set timezone manually (commented line).

    Little example of mine:

    import java.text.DateFormat;
    import java.util.Date;
    import java.util.Locale;
    import java.util.TimeZone;
    
    public class Main {
        public static void main(String[] args) {
            Locale locale = Locale.getDefault();
            TimeZone localTimeZone = TimeZone.getDefault(); 
            //TimeZone localTimeZone = TimeZone.getTimeZone("Australia/Sydney");
            DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locale);
            dateFormat.setTimeZone(localTimeZone);
            Date rightNow = new Date();
            System.out.println(locale.toString() + ": " + dateFormat.format(rightNow));
        }
    }
    

提交回复
热议问题