java 日历的 GregorianCalendar 方法将日期设置为前一天

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5567609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:42:04  来源:igfitidea点击:

Calendar's GregorianCalendar methods setting date to previous day

javacalendargregorian-calendar

提问by Thomas Buckley

I'm having a strange issue with abstract Calendar class using the GregorianCalendar method.

我在使用 GregorianCalendar 方法的抽象 Calendar 类中遇到了一个奇怪的问题。

For some reason using "calendar.set" is returning a date from the previous day. See code below for example with comments on where it works fine, and where it goes wrong.

出于某种原因,使用“calendar.set”会返回前一天的日期。例如,请参阅下面的代码,并对其工作正常以及出错的地方进行评论。

private Date checkDate(Date d, int hour, int minute, int sec, int milliSec)
{
    // Test values for arguments
    d = "Wed Apr 06 00:00:00 BST 2011";
    hour = minute = sec = milliSec = 0;

    Calendar calendar = new GregorianCalendar(sun.util.calendar.ZoneInfo[id="UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]);
    calendar.setTime(d);

    // *** OK *** calendar.getTime() will display correctly here (Wed Apr 06 00:00:00 BST 2011)

    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, minute);
    calendar.set(Calendar.SECOND, sec);
    calendar.set(Calendar.MILLISECOND, milliSec);

    // *** NOT OK *** calendar.getTime() will display inncorrectly here (Tue Apr 05 01:00:00 BST 2011)


return calendar.getTime();  

} Any ideas?

} 有任何想法吗?

Thanks

谢谢

回答by jarnbjo

Calendar calendar = new GregorianCalendar(sun.util.calendar.ZoneInfo[id="UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]);
calendar.setTime(d);

Ok, so you have a calendar with its timezone set to UTC and set its time to

好的,所以你有一个时区设置为 UTC 的日历,并将其时间设置为

Wed Apr 06 00:00:00 BST 2011.

Wed Apr 06 00:00:00 BST 2011.

Internally, this is mapped to

在内部,这映射到

2011-04-05 23:00:00 UTC(the date and time is adopted, the time zone is kept).

2011-04-05 23:00:00 UTC(采用日期和时间,保留时区)。

Setting the hour, minute, second and millisecond fields of the calendar to 0 will change it to

将日历的小时、分钟、秒和毫秒字段设置为 0 会将其更改为

2011-04-05 00:00:00 UTC.

2011-04-05 00:00:00 UTC.

If you now convert the calendar to a java.util.Dateobject and print it while considering your local time zone (BST), the output will be:

如果您现在将日历转换为java.util.Date对象并在考虑本地时区 (BST) 的情况下打印它,则输出将为:

2011-04-05 01:00:00 BST.

2011-04-05 01:00:00 BST.

回答by Riccardo Cossu

Calendar is a bit strange, as you can read in its javadoc; the bottom line is that you have to call calendare.get() after every time you call a calendar.set(...) or the modification may be inconsistent.

Calendar 有点奇怪,你可以在它的 javadoc 中读到;最重要的是,您必须在每次调用 calendar.set(...) 之后调用 calendare.get() 否则修改可能不一致。