当我在JDeveloper中运行以下java代码时,我得到了错误的输出。原因是什么呢?
System.out.println(TimeZone.getTimeZone("Australia/Sydney").inDaylightTime(new Date()));
System.out.println(TimeZone.getTimeZone("Europe/Moscow").inDaylightTime(new Date()));
我期望输出如下:真正的假
但是,我得到它作为:假真正的
PS:这是正确的工作在少数其他时区,如"美国/福特韦恩"one_answers"欧洲/柏林"。
由于new Date()
的使用并没有完全遵循如何创建最小,完整和可验证示例的原则,因此它是:
public static void main(String[] args) {
Date date = new Date(1444817534489L); // 2015-10-14 10:12:14.489 UTC
printDate(date, "UTC");
printDate(date, "Australia/Sydney");
printDate(date, "Europe/Moscow");
printDate(date, "Europe/Berlin");
printDate(date, "America/Fort_Wayne");
}
private static void printDate(Date date, String timeZoneID) {
TimeZone timeZone = TimeZone.getTimeZone(timeZoneID);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z");
df.setTimeZone(timeZone);
System.out.printf("%-20s %-30s inDaylightTime: %s%n", timeZoneID + ":",
df.format(date), timeZone.inDaylightTime(date));
}
输出UTC: 2015-10-14 10:12:14.489 UTC inDaylightTime: false
Australia/Sydney: 2015-10-14 21:12:14.489 AEDT inDaylightTime: true
Europe/Moscow: 2015-10-14 13:12:14.489 MSK inDaylightTime: false
Europe/Berlin: 2015-10-14 12:12:14.489 CEST inDaylightTime: true
America/Fort_Wayne: 2015-10-14 06:12:14.489 EDT inDaylightTime: true
所以,true false
如你所料。如果你看到的不一样,也许你的日期不一样,如果不打印出来,你怎么知道呢?