考虑到夏令时,将本地时间转换为UTC时间或相反 [英] Converting local time to UTC time or vice versa considering daylight saving time

查看:706
本文介绍了考虑到夏令时,将本地时间转换为UTC时间或相反的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何将本地时间转换为UTC时间,反之亦然。
但是我对这样做时的夏令时处理非常困惑。

I know how to convert local time to UTC time and vice versa. But I am very much confused about daylight savings time(DST) handling while doing this.

任何人都可以回答以下问题:

1.当在时区之间转换时,java内部处理DST?

2.在时区之间转换时需要做什么?

3.任何解释这个问题的好文章更清楚?

So can anyone answer the below questions:
1. Does java internally handle DST when converting between timezones?
2. What things I need to do while converting between timezones?
3. Any good article which explains about this more clearly?

提前感谢。

推荐答案

你知道如何将日期转换为UTC和回来吗?正确?

恐怕,我怀疑。

Are you sure you know how to convert dates to UTC and back? Correctly?
I am afraid, I doubt that.


  1. 是的。

  2. 您不需要转换,只需要分配正确的TimeZone。

  3. 您需要哪篇文章?好的,我正在做这个,但现在让我在这里回答。

  1. Yes.
  2. You don't need to convert, you just need to assign correct TimeZone.
  3. What you need an article for? OK, I am working on this, but for now let me put an answer here.

第一件事。您的程序应该在UTC TimeZone内部存储日期(或日历)。嗯,事实上在GMT,因为在Java没有闰秒,但这是另一个故事。

唯一的地方,当你应该需要转换,是当你要显示给用户的时间。这也关注发送电子邮件。在这两种情况下,您都需要 格式日期才能获得其文字表示。要使用 DateFormat 并分配正确的TimeZone:

The first thing first. Your program should store Date (or Calendar) in UTC TimeZone internally. Well, in fact in GMT, because there are no leap seconds in Java, but that is another story.
The only place when you should be in need of "converting", is when you are going to display the time to user. That regards to sending email messages as well. In both cases you need to format date to get its textual representation. To that you would use DateFormat and assign correct TimeZone:

    // that's for desktop application
    // for web application one needs to detect Locale
    Locale locale = Locale.getDefault();
    // again, this one works for desktop application
    // for web application it is more complicated
    TimeZone currentTimeZone = TimeZone.getDefault();
    // in fact I could skip this line and get just DateTime instance,
    // but I wanted to show how to do that correctly for
    // any time zone and locale
    DateFormat formatter = DateFormat.getDateTimeInstance(
            DateFormat.DEFAULT,
            DateFormat.DEFAULT,
            locale);
    formatter.setTimeZone(currentTimeZone);

    // Dates "conversion"
    Date currentDate = new Date();
    long sixMonths = 180L * 24 * 3600 * 1000;
    Date inSixMonths = new Date(currentDate.getTime() + sixMonths);

    System.out.println(formatter.format(currentDate));
    System.out.println(formatter.format(inSixMonths));
    // for me it prints
    // 2011-05-14 16:11:29
    // 2011-11-10 15:11:29

    // now for "UTC"
    formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
    System.out.println(formatter.format(currentDate));
    System.out.println(formatter.format(inSixMonths));
    // 2011-05-14 14:13:50
    // 2011-11-10 14:13:50

如你所见,Java关心处理DST。您当然可以手动处理,只需阅读 TimeZone相关的JavaDoc

As you can see, Java cares about handling DST. You can of course handle it manually, just read the TimeZone related JavaDoc.

这篇关于考虑到夏令时,将本地时间转换为UTC时间或相反的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆