在将毫秒数添加到java.util.date对象时出现错误结果。

huangapple go评论88阅读模式
英文:

Wrong results when adding milliseconds to java.util.date object

问题

当前时间是 Sat Apr 04 15:02:00 AEST 2020

在下面的代码片段中,我创建了一个 Date 对象,并将 86400000L 毫秒(1 天)添加到它:

Date date = new Date();
date.setTime(date.getTime() + 86400000L);
System.out.println(date);

输出结果是 Sun Apr 05 14:02:00 AEST 2020。我不明白为什么结果只添加了 23 小时到我的当前时间,而不是 24 小时。

希望能提供帮助。

英文:

Current time is Sat Apr 04 15:02:00 AEST 2020.

In the following snippet, I create a Date object and add 86400000L milliseconds (1 day) to it:

Date date = new Date();
date.setTime(date.getTime() + 86400000L);
System.out.println(date);

The output is Sun Apr 05 14:02:00 AEST 2020. I don't understand why the result adds only 23 hours to my current time, instead of 24 hours.

Any help would be appreciated.

答案1

得分: 2

这段代码运行得很好。你输出的 AEST 表示日期是指 澳大利亚东部标准时间。搜索关于 AEST 夏令时 的内容显示,在2020年4月5日星期日凌晨3:00,时钟会“倒退”1小时。因此,在夏令时改变之前的24小时内,只会使时间提前23小时。

如果你明天运行这段代码,就不会有这个“问题”。

英文:

The code works just fine. The AEST on your output means that the date regards Australian Eastern Standard Time. Googling for AEST dst shows that on Sunday, April 5, 3:00 am 2020 the clock will "go back" 1 hour. Thus adding 24 hours just before the DST change, will only move the time 23 hours forward.

If you run that code tomorrow, you'll not have this "problem".

答案2

得分: 1

使用java.time,现代的Java日期和时间API,来处理你的日期和时间工作。

ZonedDateTime currentTime = ZonedDateTime.now(ZoneId.of("Australia/Sydney"));
System.out.println(currentTime);
ZonedDateTime tomorrowSameTime = currentTime.plusDays(1);
System.out.println(tomorrowSameTime);

运行时的输出:

>     2020-04-04T16:00:30.579484+11:00[Australia/Sydney]
>     2020-04-05T16:00:30.579484+10:00[Australia/Sydney]

请注意:我们得到了明天相同的时刻,即16:00。因为夏时制(夏令时)结束,明天的UTC偏移不同,从+11:00变为+10:00。而且,重要的是,虽然我认为+ 86400000L几乎难以阅读,用.plusDays(1)可以非常清楚地传达意图。

如果需要,请插入另一个澳大利亚东部时区。

你的代码哪里出错了? cherouvim在另一个回答中已经很好地解释了这一点,我不需要重复。我只允许我补充一点,Date类不仅设计得很糟糕,导致了你的困惑,而且它也已经过时了。我建议你不要使用它。正如cherouvim在评论中指出的那样,使用日期进行编程是很困难的。不要相信你可以自己将1天转换为86400000毫秒。将所有的日期和时间计算留给经过验证的库方法。

链接: Oracle教程:日期时间 解释了如何使用java.time。

英文:

Do use java.time, the modern Java date and time API, for your date and time work.

	ZonedDateTime currentTime = ZonedDateTime.now(ZoneId.of("Australia/Sydney"));
	System.out.println(currentTime);
	ZonedDateTime tomorrowSameTime = currentTime.plusDays(1);
	System.out.println(tomorrowSameTime);

Output when running just now:

> 2020-04-04T16:00:30.579484+11:00[Australia/Sydney]
> 2020-04-05T16:00:30.579484+10:00[Australia/Sydney]

Please observe: we got the same time of day tomorrow, 16:00. Because summer time (daylight saving time) ends, the UTC offset for tomorrow is different, +10:00 instead of +11:00. And importantly, while I find + 86400000L pretty close to unreadable for adding a day, .plusDays(1) conveys the intention very clearly.

Please insert a different Eastern Australian time zone if required.

What went wrong in your code? cherouvim has explained this very nicely in the other answer, no need for me to repeat. Only allow me to add that the Date class is not only poorly designed — giving rise to your confusion — it is also long outdated. I recommend you don’t use it. And as cherouvim notes in a comment, programming with dates is hard. Don’t trust that you can yourself convert 1 day to 86 400 000 milliseconds. Leave all date and time calculations to proven library methods.

Link: Oracle tutorial: Date Time explaining how to use java.time.

huangapple
  • 本文由 发表于 2020年4月4日 12:29:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/61023766.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定