以毫秒为单位获得实际的夏令时节省
我正在 Java Swing 中制作模拟时钟。要计算时钟指针角度,我需要:
- UTC 时间(以毫秒为单位),我可以通过 System.currentTimeMillis() 获得
- TimeZone 偏移
- TimeZone DST 节省。
对于2)和3)我想使用 TimeZone.getDefault().getRawOffset()
和 TimeZone.getDefault().getDSTSavings()
。
但无论当前日期是冬令时还是夏令时,getDSTavings()
始终返回 3600000
。
看起来它只检查该区域是否处于 DST
管理之下,如果是,则返回 3600000
,否则返回 0
。
这是 getDSTavings()
中的错误吗?
如果不是,那么获取当前DST
(以毫秒为单位)校正的唯一方法是使用日历实例,例如:Calendar.getInstance.get(Calendar.DST_OFFSET)
?
Calendar cal1 = Calendar.getInstance(TimeZone.getDefault()); //currentZone: CET/CEST +1/+2, GMT+1:00
System.out.println("System time, " + System.currentTimeMillis()); //UTC current milis
System.out.println("Calendar time, " + cal1.getTime().getTime()); //UTC current milis
System.out.println("Calendar milis, " + cal1.getTimeInMillis()); //UTC current milis
System.out.println("Calendar Zone Offset: " + cal1.get(Calendar.ZONE_OFFSET));
System.out.println("Calendar DST Offset: " + cal1.get(Calendar.DST_OFFSET));
System.out.println("Calendar Zone Offset: " + cal1.getTimeZone().getRawOffset());
System.out.println("Calendar DST Offset: " + cal1.getTimeZone().getDSTSavings());
System.out.println("");
// Winter time, CET
cal1.set(2010, 11, 15, 14, 15, 5);
System.out.println("Calendar milis, " + cal1.getTimeInMillis()); //UTC
System.out.println("Calendar Zone Offset: " + cal1.get(Calendar.ZONE_OFFSET)); // 3600000 correct
System.out.println("Calendar DST Offset: " + cal1.get(Calendar.DST_OFFSET)); // 0 correct
System.out.println("Calendar Zone Offset: " + cal1.getTimeZone().getRawOffset()); // 3600000 correct
System.out.println("Calendar DST Offset: " + cal1.getTimeZone().getDSTSavings()); // 3600000 wrong !!!
System.out.println("");
// Summer time - CEST
cal1.set(2010, 8, 15, 14, 15, 5);
System.out.println("Calendar milis, " + cal1.getTimeInMillis()); //UTC
System.out.println("Calendar Zone Offset: " + cal1.get(Calendar.ZONE_OFFSET)); // 3600000 correct
System.out.println("Calendar DST Offset: " + cal1.get(Calendar.DST_OFFSET)); // 3600000 correct
System.out.println("Calendar Zone Offset: " + cal1.getTimeZone().getRawOffset()); // 3600000 correct
System.out.println("Calendar DST Offset: " + cal1.getTimeZone().getDSTSavings()); // 3600000 correct
I'm making analog clock in Java Swing. To calculate clock pointer angles I need:
- UTC time in milliseconds which I can get with
System.currentTimeMillis()
- TimeZone offset
- TimeZone DST savings.
For 2) and 3) I thought of usingTimeZone.getDefault().getRawOffset()
and TimeZone.getDefault().getDSTSavings()
.
But getDSTSavings()
always returns 3600000
regardless of current date is in winter/summer time.
It looks like it only checks to see if that zone is under DST
management and if so it returns 3600000
otherwise 0
.
Is this a bug in getDSTSavings()
?
If not then only way to get current DST
correction in miliseconds is to use Calendar instance like: Calendar.getInstance.get(Calendar.DST_OFFSET)
?
Calendar cal1 = Calendar.getInstance(TimeZone.getDefault()); //currentZone: CET/CEST +1/+2, GMT+1:00
System.out.println("System time, " + System.currentTimeMillis()); //UTC current milis
System.out.println("Calendar time, " + cal1.getTime().getTime()); //UTC current milis
System.out.println("Calendar milis, " + cal1.getTimeInMillis()); //UTC current milis
System.out.println("Calendar Zone Offset: " + cal1.get(Calendar.ZONE_OFFSET));
System.out.println("Calendar DST Offset: " + cal1.get(Calendar.DST_OFFSET));
System.out.println("Calendar Zone Offset: " + cal1.getTimeZone().getRawOffset());
System.out.println("Calendar DST Offset: " + cal1.getTimeZone().getDSTSavings());
System.out.println("");
// Winter time, CET
cal1.set(2010, 11, 15, 14, 15, 5);
System.out.println("Calendar milis, " + cal1.getTimeInMillis()); //UTC
System.out.println("Calendar Zone Offset: " + cal1.get(Calendar.ZONE_OFFSET)); // 3600000 correct
System.out.println("Calendar DST Offset: " + cal1.get(Calendar.DST_OFFSET)); // 0 correct
System.out.println("Calendar Zone Offset: " + cal1.getTimeZone().getRawOffset()); // 3600000 correct
System.out.println("Calendar DST Offset: " + cal1.getTimeZone().getDSTSavings()); // 3600000 wrong !!!
System.out.println("");
// Summer time - CEST
cal1.set(2010, 8, 15, 14, 15, 5);
System.out.println("Calendar milis, " + cal1.getTimeInMillis()); //UTC
System.out.println("Calendar Zone Offset: " + cal1.get(Calendar.ZONE_OFFSET)); // 3600000 correct
System.out.println("Calendar DST Offset: " + cal1.get(Calendar.DST_OFFSET)); // 3600000 correct
System.out.println("Calendar Zone Offset: " + cal1.getTimeZone().getRawOffset()); // 3600000 correct
System.out.println("Calendar DST Offset: " + cal1.getTimeZone().getDSTSavings()); // 3600000 correct
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是如何使用 DST 的示例:
如果伦敦在指定日期实行夏令时,则将打印 3600000。否则会打印0。
Here is an example of how DST is used:
This will print 3600000 if Daylight Saving Time is in effect in London at the specified date. Otherwise it will print 0.
您在第一行检索的 cal1 实例采用 GMT+1,并且无论您设置什么时间/日期,它都不会改变。如果您需要在时钟的每个刻度上检查当前时区,则需要在每个刻度上检索一个新的 Calendar 实例。
顺便说一句,如果您愿意向项目添加依赖项 - Joda Time 是一个很棒的日期和时间库。
The cal1 instance the you retrieve on the first line is in GMT+1 and that does not change regardless of what time / date you set on it. If you need to check the current timezone on every tick of the clock, you will need to retrieve a new Calendar instance at each tick.
As a side-note, if you're willing to add a dependency to your project - Joda Time is a wonderful Date and Time library.