【发布时间】:2014-09-09 09:07:44
【问题描述】:
当我使用 java6 在 windows 上运行以下代码时,它会打印以下输出。
private static SimpleDateFormat timeZoneFormatter = new SimpleDateFormat("Z");
public static void main(String[] args) {
try {
String format = "yyyyMMdd";
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
Date date1 = dateFormat.parse("20140330");
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(date1);
Date date2 = dateFormat.parse("20140401");
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(date2);
calendar1.setTimeZone(TimeZone.getTimeZone("GMT"));
calendar2.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(timeZoneFormatter.format(calendar1.getTime()));
System.out.println(timeZoneFormatter.format(calendar2.getTime()));
} catch (Exception e) {
e.printStackTrace();
}
}
+0200
+0300
据我所知,无论我在日历对象上设置什么时区,它都需要 SimpleDateFormat 对象上的默认时区信息。我在这里错过了什么吗?如果这是正常行为,在什么情况下我们应该使用 Calendar.setTimezone() 方法?
【问题讨论】: