Java Date and Daylight Saving

丶灬走出姿态 提交于 2019-12-06 13:58:49

You probably want a DateFormat object that is set up for UTC (which does not observe daylight savings), using a calendar set to UTC will also simplify things quite a bit.

public static void main(String[] args) {
    TimeZone utc = TimeZone.getTimeZone("UTC");
    Calendar date = Calendar.getInstance(utc);
    DateFormat format = DateFormat.getDateTimeInstance();
    format.setTimeZone(utc);
    date.set(2012, 02, 24, 23, 00, 00);
    for (int i = 0; i < 10; i++) {
        System.out.println(format.format(date.getTime()));
        date.add(Calendar.HOUR_OF_DAY, 1);
    }
}

This provides the following output:

Mar 24, 2012 11:00:00 PM
Mar 25, 2012 12:00:00 AM
Mar 25, 2012 1:00:00 AM
Mar 25, 2012 2:00:00 AM
Mar 25, 2012 3:00:00 AM
Mar 25, 2012 4:00:00 AM
Mar 25, 2012 5:00:00 AM
Mar 25, 2012 6:00:00 AM
Mar 25, 2012 7:00:00 AM
Mar 25, 2012 8:00:00 AM

Of course, you can use the formatter to format your date however you like.

The Calendar class allows you to specify a time zone when you convert Date values. See the API doc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!