Java日期和夏令时

编程入门 行业动态 更新时间:2024-10-25 18:23:36
本文介绍了Java日期和夏令时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用for循环打印一天中的所有时间,并增加一个Date对象。 但我不能有太阳3月25日02:00:00,我只有这些:

I'm trying to print all the hours of the day using a for loop and incrementing a Date object. But I can't have "Sun Mar 25 02:00:00", I only have these:

Sun Mar 25 01:00:00 CET 2012 Sun Mar 25 03:00:00 CEST 2012 Sun Mar 25 03:00:00 CEST 2012 Sun Mar 25 04:00:00 CEST 2012 Sun Mar 25 05:00:00 CEST 2012

我不感兴趣在有时区。 我认为问题是由于夏令时,但我需要3月25日02:00:00。 如何创建该日期?

I'm not interested in having the TimeZone. I think that the problem is due to the Daylight Saving, but I need the "Sun Mar 25 02:00:00". How can I do to create that date?

推荐答案

您可能需要为UTC设置的DateFormat对象(不遵守夏令时),使用设置为UTC的日历也将简化事情。

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); } }

提供以下输出:

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.

更多推荐

Java日期和夏令时

本文发布于:2023-10-28 08:23:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1536082.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:夏令时   日期   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!