Java SimpleDateFormat Wrong Timezone after parsing

Why when i give input date string with GMT timezone, SimpleDateFormat parses it and outputs 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));

And output is: Mon Oct 31 13:19:56 EET 2011 rather than Mon Oct 31 13:19:56 GMT 2011


You're printing out the result of Date.toString() . A Date doesn't have any concept of a timezone - it's just the number of milliseconds since the UTC Unix epoch. Date.toString() always uses the system default time zone.

Note that you shouldn't be expecting "Mon Oct 31 13:19:56 GMT 2011" given that you've given a time which specifies a GMT hour of 11, not 13.

If you want to use a specific time zone for printing, you should use another DateFormat for the printing, rather than using Date.toString() . ( Date.toString() keeps causing confusion like this; it's really unfortunate.)

链接地址: http://www.djcxy.com/p/59286.html

上一篇: 将Java字符串解析为GMT日期

下一篇: Java SimpleDateFormat解析后错误的时区