获取 DST 时间时遇到问题?

发布于 2024-11-18 12:08:10 字数 143 浏览 6 评论 0原文

我试图通过使用获取其他 GMT 值的时间

Calendar c1 = Calendar.getInstance(TimeZone.getTimeZone(gmt));

,但如何获取该时区的 DST 时间。

I am trying to get the time of other GMT value by using

Calendar c1 = Calendar.getInstance(TimeZone.getTimeZone(gmt));

but how can i get the DST time at that time zone.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

南薇 2024-11-25 12:08:10

TimeZone 类为特定 TimeZone 对象提供 getDSTavings() 方法。 (JavaDoc:“返回要添加到本地标准时间以获得本地挂钟时间的时间量。”)

Calendar 接口提供了两个 getOffset() 方法,可让您找出与 UTC 的偏移量。 (JavaDoc:“返回指定日期该时区相对于UTC的偏移量。如果指定日期实行夏令时,则偏移值将根据夏令时的量进行调整。”)

请看这段代码理解java时间的复杂方式:

@Test
public void testDST() {
    final TimeZone met = TimeZone.getTimeZone("MET");
    Calendar gc = new GregorianCalendar(met);
    final long timeInMillis = gc.getTimeInMillis();
    final long gmtTime= timeInMillis-(gc.getTimeZone().getOffset(timeInMillis));
    final Date gmtDate = new Date(gmtTime);
    System.out.printf("%-40s: %tc\n%-40s: %tc\n%-40s: %tc\n%-40s: %d\n%-40s: %d",
            "new Date() (local timezone)",new Date(), 
            "UTC", gmtDate , 
            "now from Calendar with TC GMT+02:00",gc,
            "zoneoffset",gc.get(Calendar.ZONE_OFFSET), 
            "dst savings",met.getDSTSavings());

}

您还可以定义自己的SimpleTimeZone并提供自定义DST规则,但是,我还没有找到如何从预定义的TimeZones中获取此信息。

您还应该注意,如果 TimeZone.getTimeZone(TZName) 没有找到指定的时区,它不会抛出异常,但它只是使用 GMT,这可能会导致重大误解。

您可以在日历、时区、日期等的 javadoc 中找到所有这些信息(以及更多信息)。

The TimeZone class provides a getDSTSavings() method for a specific TimeZone Object. (JavaDoc: "Returns the amount of time to be added to local standard time to get local wall clock time.")

The Calendar interface provides two getOffset() methods, which let you find out the offset from UTC. (JavaDoc: "Returns the offset of this time zone from UTC at the specified date. If Daylight Saving Time is in effect at the specified date, the offset value is adjusted with the amount of daylight saving. ")

please see this piece of code to grok the complicated ways of java time:

@Test
public void testDST() {
    final TimeZone met = TimeZone.getTimeZone("MET");
    Calendar gc = new GregorianCalendar(met);
    final long timeInMillis = gc.getTimeInMillis();
    final long gmtTime= timeInMillis-(gc.getTimeZone().getOffset(timeInMillis));
    final Date gmtDate = new Date(gmtTime);
    System.out.printf("%-40s: %tc\n%-40s: %tc\n%-40s: %tc\n%-40s: %d\n%-40s: %d",
            "new Date() (local timezone)",new Date(), 
            "UTC", gmtDate , 
            "now from Calendar with TC GMT+02:00",gc,
            "zoneoffset",gc.get(Calendar.ZONE_OFFSET), 
            "dst savings",met.getDSTSavings());

}

You can also define your own SimpleTimeZone and provide custom DST rules, however, i have not found out how to get this information from the predefined TimeZones.

You should also be aware, that if TimeZone.getTimeZone(TZName) does not find the specified timezone, it does not throw an exception, but it just uses GMT, which can cause major misunderstandings.

You can find all this information (and a lot more) in javadoc for Calendar, TimeZone, Date, etc.

哭泣的笑容 2024-11-25 12:08:10

There are few methods available in java.util.TimeZone to get Daylight Saving Time. Please check out the BlackBerry Java Docs page.

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