完全混淆了 java 时间的变化以及为什么有数千篇与此相关的帖子,我以某种方式设法使用 jodaTime 将时间存储在 UTC 中:
Calendar cal = new GregorianCalendar();
cal.setTimeInMillis(DateTimeZone.getDefault().convertLocalToUTC(
cal.getTimeInMillis(), false));
由于我使用的是休眠(并且不愿意使用适配器让 JodaTime 与版本 4 一起正常工作),我只使用这个 jodaTime 方法将 jdk 时间转换为 utc。这似乎产生了预期的结果,并且本地时间(当前伦敦 GMT+1)通过从本地时间减去 1 转换为 UTC。
到现在为止还挺好。然后,每当我检索特定时区的时间时,我都会得到不正确的偏移量 -2,它应该是 -3,包括 DST。
日历 cal = new GregorianCalendar();
System.out.println("Local London Hours: "+cal.get(Calendar.HOUR_OF_DAY)); System.out.println("Local London Milliseconds: "+cal.getTimeInMillis()); cal.setTimeInMillis(DateTimeZone.getDefault().convertLocalToUTC( cal.getTimeInMillis(), false)); System.out.println("UTC Hours: "+cal.get(Calendar.HOUR_OF_DAY)); System.out.println("UTC Milliseconds: "+cal.getTimeInMillis()); // Time for specific time zone cal.setTimeZone(TimeZone.getTimeZone("Europe/Vilnius")); System.out.println("Vilnius Hours: "+cal.get(Calendar.HOUR_OF_DAY)); System.out.println("Vilnius Milliseconds: "+cal.getTimeInMillis()); // is this time in DST? - Yes System.out.println("Vilnius time is in DST: "+TimeZone.getTimeZone("Europe/Vilnius").inDaylightTime(cal.getTime()));
http://www.timeanddate.com/worldclock/city.html?n=660 特定时区的时区详细信息
输出:
伦敦当地时间:21
Local London Milliseconds: 1381869901339
UTC Hours: 20
UTC Milliseconds: 1381866301339
Vilnius Hours: 22 // this should be 23 (according to link)
Vilnius Milliseconds: 1381866301339
Vilnius time is in DST: true // no, it is not since hours value is not 23