日历的 GregorianCalendar 方法将日期设置为前一天
我在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,您的日历的时区设置为 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
。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
.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.