以毫秒为单位获得实际的夏令时节省

发布于 2024-10-01 22:10:30 字数 2429 浏览 3 评论 0原文

我正在 Java Swing 中制作模拟时钟。要计算时钟指针角度,我需要:

  1. UTC 时间(以毫秒为单位),我可以通过 System.currentTimeMillis() 获得
  2. TimeZone 偏移
  3. 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:

  1. UTC time in milliseconds which I can get with System.currentTimeMillis()
  2. TimeZone offset
  3. TimeZone DST savings.

For 2) and 3) I thought of using
TimeZone.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜是你 2024-10-08 22:10:30

以下是如何使用 DST 的示例:

TimeZone london = TimeZone.getTimeZone("Europe/London");
System.out.println(london.getOffset(date.getTime()));

如果伦敦在指定日期实行夏令时,则将打印 3600000。否则会打印0。

Here is an example of how DST is used:

TimeZone london = TimeZone.getTimeZone("Europe/London");
System.out.println(london.getOffset(date.getTime()));

This will print 3600000 if Daylight Saving Time is in effect in London at the specified date. Otherwise it will print 0.

浅听莫相离 2024-10-08 22:10:30

您在第一行检索的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文