1976年3月28日至3月29日与java.util.GregorianCalendar发生了什么? [英] What happened between March 28th and March 29th, 1976 with the java.util.GregorianCalendar?

查看:150
本文介绍了1976年3月28日至3月29日与java.util.GregorianCalendar发生了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图使用GregorianCalendar,计算自特定日期以来的天数,我陷入奇点。
在scala解释器中,我输入:

Trying to use the GregorianCalendar, I got stuck on a singularity while computing the number of days since a particular date. In the scala interpreter, I entered :

scala>import java.util.GregorianCalendar
scala>import java.util.Calendar
scala>val dateToday = new GregorianCalendar(2012,Calendar.MAY,22).getTimeInMillis()
dateToday: Long = 1337637600000
scala>val days1 = (dateToday - (new GregorianCalendar(1976,Calendar.MARCH,28).getTimeInMillis())) / (1000*3600*24)
days1: Long = 13203
scala>val days2 = (dateToday - (new GregorianCalendar(1976,Calendar.MARCH,29).getTimeInMillis())) / (1000*3600*24)
days2: Long = 13203

我不知道1976年是一个闰年的事实,但是第1天和第2天应该被分开1.这是自1970年以来唯一的历史时刻,发现奇点。

I don't know if the fact that 1976 is a leap year matters, but days1 and days2 should have been separated by 1. This is the only moment in history since 1970 that this singularity happens.

想知道发生了什么,我计算了之前提到的两个日期之间的差异,它给了我只有23个小时的差异!那天发生了什么事?维基百科显然没有说出来。

Wanting to know what is going on, I compute the difference between the two dates previously mentionned, and it gives me only exactly 23 hours of difference ! What happened on that date ? Wikipedia apparently says nothing about it.

更重要的是,如何计算特定日期之后的实际天数?

And even more important, how to compute the real number of days since a particular date ?

推荐答案

如果浮动部分的选项不可用,我可以想到的最好的答案是在计算两天之间的差距时添加一个小时(1000 * 3600) :

If the option of a float division is not available, the best answer I can think of is to add one hour (1000*3600) while computing a difference between two days:

scala> (dateToday - (new GregorianCalendar(1976, Calendar.MARCH, 28).getTimeInMillis()) + 1000*3600) / (1000 * 3600 * 24)
days1: Long = 13204
scala> (dateToday - (new GregorianCalendar(1976, Calendar.MARCH, 29).getTimeInMillis()) + 1000*3600) / (1000 * 3600 * 24)
days1: Long = 13203

它应该适用于每个日期,因为您不再遭受闰年。

It should work for every date, as you do not suffer from leap hours anymore.

这篇关于1976年3月28日至3月29日与java.util.GregorianCalendar发生了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆