java.util.Calendar 未报告正确的 timeInMillis

发布于 2024-08-14 18:07:46 字数 1532 浏览 4 评论 0原文

findCalendarStart: time into Calendar: 1260575897

findCalendarStart: set hour : 13

findCalendarStart: after hour : 1249775897

findCalendarStart: after hour string: Thu Jan 15 11:09:35 UTC 1970

findCalendarStart: set minutes  : 13

findCalendarStart: after minutes: 1250015897

findCalendarStart: what calendar returns: 1250015897

我在日历中放置了一个日期(通过从今天开始经过一毫秒长的时间来初始化)。日历已正确初始化。在第一个计算中,我将一天中的小时更改为 13。此时,startCalTime.set(Calendar.HOUR_OF_DAY, ((new Integer(m.group(1)).intValue())*2)-1 );

我正在传递正确的一天中的小时值和分钟值,因为我在记录器中看到它们。在我只更改今天的 Date 对象中的小时后,什么可能导致日历出现如此奇怪的日期?

更多代码:

Calendar startCalTime = Calendar.getInstance(TimeZone.getTimeZone("America/Los_Angeles"));
Date d = new Date(creationTime);
startCalTime.setTime(d);       
 startCalTime.getTimeInMillis();

..regex..

if(m.find()){
//SET HOUR OF DAY
_logger.warning("set hour 1 : " + new Integer((new Integer(m.group(1)).intValue())-1));                         startCalTime.set(Calendar.HOUR_OF_DAY, new Integer(m.group(1)).intValue()-1 );
_logger.warning("after hour 1:  " + new Long(startCalTime.getTime().getTime()));
_logger.warning("after hour 1 string:  " + startCalTime.getTime().toString());  
//SET MINUTE
_logger.warning("set minutes 1 : " + new Integer(m.group(2).toString()));
startCalTime.set(Calendar.MINUTE, new Integer(m.group(2)).intValue());
_logger.warning("after minutes 1:  " + new Long(startCalTime.getTime().getTime()));}

谢谢,

culov

findCalendarStart: time into Calendar: 1260575897

findCalendarStart: set hour : 13

findCalendarStart: after hour : 1249775897

findCalendarStart: after hour string: Thu Jan 15 11:09:35 UTC 1970

findCalendarStart: set minutes  : 13

findCalendarStart: after minutes: 1250015897

findCalendarStart: what calendar returns: 1250015897

I place a Date (initialized by passing long from a millisecond from today) in a Calendar. Calendar is correctly initialized. In the first calculation, I change the hour of day to 13. At this point, startCalTime.set(Calendar.HOUR_OF_DAY, ((new Integer(m.group(1)).intValue())*2)-1 );

I am passing the right hour of day values and minutes because Im seeing them in the logger. What could possibly be causing calendar to come up with such strange dates after I only change the hour of day from todays Date object?

More code:

Calendar startCalTime = Calendar.getInstance(TimeZone.getTimeZone("America/Los_Angeles"));
Date d = new Date(creationTime);
startCalTime.setTime(d);       
 startCalTime.getTimeInMillis();

..regex..

if(m.find()){
//SET HOUR OF DAY
_logger.warning("set hour 1 : " + new Integer((new Integer(m.group(1)).intValue())-1));                         startCalTime.set(Calendar.HOUR_OF_DAY, new Integer(m.group(1)).intValue()-1 );
_logger.warning("after hour 1:  " + new Long(startCalTime.getTime().getTime()));
_logger.warning("after hour 1 string:  " + startCalTime.getTime().toString());  
//SET MINUTE
_logger.warning("set minutes 1 : " + new Integer(m.group(2).toString()));
startCalTime.set(Calendar.MINUTE, new Integer(m.group(2)).intValue());
_logger.warning("after minutes 1:  " + new Long(startCalTime.getTime().getTime()));}

Thanks,

culov

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

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

发布评论

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

评论(3

墨落成白 2024-08-21 18:07:46

让我们看看如何初始化日期。我怀疑您传递的不是毫秒,而是自纪元开始以来的 - 这(秒,而不是毫秒)是常规 Unix 时间戳的定义方式。 Java 使用毫秒来获得更好的粒度。

Let's see how you initialize your date. I suspect that instead of milliseconds, you are passing it seconds since epoch start - this (seconds, not milliseconds) is how regular Unix timestamps are defined. Java uses milliseconds for better granularity.

懒的傷心 2024-08-21 18:07:46
Calendar startCalTime = Calendar.getInstance(TimeZone.getTimeZone("America/Los_Angeles"));
Date d = new Date(creationTime);

那里会发生什么? startCalTimecreationTime 似乎没有连接,我认为它们应该是?

另外,为了获得稍微更好的性能/内存占用,请尽可能避免 new Integer/Long 并使用 Long/Integer.valueOf() 代替。

Calendar startCalTime = Calendar.getInstance(TimeZone.getTimeZone("America/Los_Angeles"));
Date d = new Date(creationTime);

What happens there? startCalTime and creationTime don't seem to be connected, I'd assume they should be?

Also for very slightly better performance/memory footprint, avoid new Integer/Long as much as possible and use Long/Integer.valueOf() instead.

装纯掩盖桑 2024-08-21 18:07:46

您日历中的那些时间看起来不太对劲。如果这些时间以毫秒为单位,那么 126..... 只代表 350 小时的时间,看起来相差了近 40 年。

原因似乎是您的初始化并未真正将日历设置为今天的日期。初始日期似乎仅过了该纪元几个小时。

请发布更多代码,我们可以为您修复它。

Those times in your Calendar don't look right. If those are supposed to be times in milliseconds, then 126..... represents a time of only 350 hours, which looks to be off by almost 40 years.

The reason seems to be that your initialization is not really setting your calendar to today's date. The initial date seems to be just a few hours past the epoch.

Please post some more code and we can fix it for you.

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