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

发布于 2024-10-30 13:10:21 字数 970 浏览 1 评论 0原文

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

由于某种原因,使用“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();  

} 有什么想法吗?

谢谢

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

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.

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

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

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

发布评论

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

评论(2

天煞孤星 2024-11-06 13:10:21
Calendar calendar = new GregorianCalendar(sun.util.calendar.ZoneInfo[id="UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]);
calendar.setTime(d);

好的,您的日历的时区设置为 UTC,时间设置为

Wed Apr 06 00:00:00 BST 2011

在内部,这被映射到

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

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

2011-04-05 00:00:00 UTC

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

2011-04-05 01:00 :00 BST

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

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).

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

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

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

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

想你只要分分秒秒 2024-11-06 13:10:21

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

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.

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