Java SimpleDateFormat Incorrect timezone after parsing

Why, when I give the date input string with GMT timezone, does SimpleDateFormat parse it and display the EET timezone?

 public static String DATE_FORMAT="dd MMM yyyy hh:mm:ss z"; public static String CURRENT_DATE_STRING ="31 October 2011 11:19:56 GMT"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.US); simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); System.out.println(simpleDateFormat.parseObject(CURRENT_DATE_STRING)); 

code>

And the conclusion: Mon Oct 31 13:19:56 EET 2011 rather than Mon Oct 31 13:19:56 GMT 2011

+6
source share
1 answer

You print the result of Date.toString() . A Date has no idea about the time zone - it's just milliseconds since the Unix UTC era. Date.toString() always uses the default system time zone.

Please note that you should not expect β€œMon Oct 31 13:19:56 GMT 2011”, given that you have specified a time that indicates the time zone is 11, not 13.

If you want to use a specific time zone for printing, you should use a different DateFormat for printing, and not use Date.toString() . ( Date.toString() continues to be confusing, like this, this is really unsuccessful.)

+10
source

Source: https://habr.com/ru/post/900425/


All Articles