关于java.util.Calendar的问题
我试图通过以下代码来理解该行为。 我的本地时区是 UTC -7(亚利桑那州)。
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MINUTE,40);
cal.set(Calendar.AM_PM,Calendar.PM);
System.out.println("1 UTC -4 Hour:" + cal.get(Calendar.HOUR_OF_DAY));
System.out.println("1 UTC -4 Day:" + cal.get(Calendar.DAY_OF_MONTH));
System.out.println("1 UTC -7 Time Stamp:" + cal.getTime().toString());
cal.set(Calendar.HOUR,12);
System.out.println("2 UTC -4 Hour:" + cal.get(Calendar.HOUR_OF_DAY));
System.out.println("2 UTC -4 Day:" + cal.get(Calendar.DAY_OF_MONTH));
System.out.println("2 UTC -7 Time Stamp:" + cal.getTime().toString());
cal.setTimeZone(TimeZone.getTimeZone("America/New_York")); //set time zone to UTC -4
System.out.println("3 UTC -4 Hour:" + cal.get(Calendar.HOUR_OF_DAY));
System.out.println("3 UTC -4 Day:" + cal.get(Calendar.DAY_OF_MONTH));
System.out.println("3 UTC -7 Time Stamp:" + cal.getTime().toString());
cal.set(Calendar.HOUR,12);
System.out.println("4 UTC -4 Hour:" + cal.get(Calendar.HOUR_OF_DAY));
System.out.println("4 UTC -4 Day:" + cal.get(Calendar.DAY_OF_MONTH));
System.out.println("4 UTC -7 Time Stamp:" + cal.getTime().toString());
它生成以下输出:
1 UTC -4 小时:12
1 UTC -4 天:27
1 UTC -7 时间戳:2011 年 9 月 27 日星期二 12:40:37 MST
2 UTC -4 小时:0
2 UTC -4 天:28
2 UTC -7 时间戳:2011 年 9 月 28 日星期三 00:40:37 MST
3 UTC -4 小时:3
3 UTC -4 天:28
3 UTC -7 时间戳:2011 年 9 月 28 日星期三 00:40:37 MST
4 UTC -4 小时:12
4 UTC -4 天:28
4 UTC -7 时间戳:2011 年 9 月 28 日星期三 09:40:37 MST
我不明白的是为什么第一个 cal.set(Calendar.HOUR,12) 会导致日期翻转到第二天。对一个值使用 add() 会导致其他值进行调整,这是有道理的,但 set() 这样做也是没有意义的。
有没有某种方法可以在保留所有其他值的情况下执行绝对 set() ?
I'm trying to understand the behavior with the following code.
My local time zone is UTC -7 (Arizona).
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MINUTE,40);
cal.set(Calendar.AM_PM,Calendar.PM);
System.out.println("1 UTC -4 Hour:" + cal.get(Calendar.HOUR_OF_DAY));
System.out.println("1 UTC -4 Day:" + cal.get(Calendar.DAY_OF_MONTH));
System.out.println("1 UTC -7 Time Stamp:" + cal.getTime().toString());
cal.set(Calendar.HOUR,12);
System.out.println("2 UTC -4 Hour:" + cal.get(Calendar.HOUR_OF_DAY));
System.out.println("2 UTC -4 Day:" + cal.get(Calendar.DAY_OF_MONTH));
System.out.println("2 UTC -7 Time Stamp:" + cal.getTime().toString());
cal.setTimeZone(TimeZone.getTimeZone("America/New_York")); //set time zone to UTC -4
System.out.println("3 UTC -4 Hour:" + cal.get(Calendar.HOUR_OF_DAY));
System.out.println("3 UTC -4 Day:" + cal.get(Calendar.DAY_OF_MONTH));
System.out.println("3 UTC -7 Time Stamp:" + cal.getTime().toString());
cal.set(Calendar.HOUR,12);
System.out.println("4 UTC -4 Hour:" + cal.get(Calendar.HOUR_OF_DAY));
System.out.println("4 UTC -4 Day:" + cal.get(Calendar.DAY_OF_MONTH));
System.out.println("4 UTC -7 Time Stamp:" + cal.getTime().toString());
It generates this output:
1 UTC -4 Hour:12
1 UTC -4 Day:27
1 UTC -7 Time Stamp:Tue Sep 27 12:40:37 MST 2011
2 UTC -4 Hour:0
2 UTC -4 Day:28
2 UTC -7 Time Stamp:Wed Sep 28 00:40:37 MST 2011
3 UTC -4 Hour:3
3 UTC -4 Day:28
3 UTC -7 Time Stamp:Wed Sep 28 00:40:37 MST 2011
4 UTC -4 Hour:12
4 UTC -4 Day:28
4 UTC -7 Time Stamp:Wed Sep 28 09:40:37 MST 2011
What I don't understand is why the first cal.set(Calendar.HOUR,12) causes the date to flip to the next day. It makes sense that using add() on one value would cause other values to be adjusted, but it doesn't make sense for set() to do that as well.
Is there some way to do an absolute set() where all other values are preserved?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您设置的是“HOUR”而不是“HOUR_OF_DAY”。因此,它将其设置为“下午开始后 12 小时” - 即当天结束时的午夜,也就是第二天的开始。可以把它想象成说,“我注定要成为 PM,所以设置时间是
中午 + 小时 * 12
”我个人认为这仍然是一个有点奇怪的行为,但我会坚持使用改为 HOUR_OF_DAY...或者最好首先使用 Joda 时间。
You're setting "HOUR" rather than "HOUR_OF_DAY". It's therefore setting it to be "12 hours after the start of the afternoon" - i.e. midnight at the end of that day, so the start of the next. Think of it as saying, "I'm meant to be PM, so setting the hour is
midday + hours * 12
"Personally I think that's still a bit odd behaviour, but I'd stick to using HOUR_OF_DAY instead... or preferrably, using Joda Time in the first place.