2012-12-02 151 views
4

爲什麼時間戳((長)-62135596800000L)返回0001-01-03 00:00:00,但必須返回0001-01-01 00:00:00?該服務顯示正確的時間戳here時間戳顯示錯誤的數據

enter image description here

+0

也許這些網站上的答案是錯的? – assylias

+0

在java中它是-62135773200000L。也許siete的計算在閏年計算上是錯誤的;格利高裏/朱利安日曆。 –

回答

12

-62135596800000爲0001-01-03 00:00:00.0因爲by default java uses Julian calendar for dates before October 15, 1582

您正在使用的網站使用的JavaScript使用所有日期的推斷或格雷戈裏歷日歷。來自javascript specification

ECMAScript使用外推陽曆系統將日數號碼映射到年號碼並確定當年的月份和日期。

事實上,在javascript:

new Date(-62135596800000).toUTCString() 
//"Mon, 01 Jan 1 00:00:00 GMT" 

您可以使用這樣的事情在Java中獲得相同的結果:

GregorianCalendar date = new GregorianCalendar(); 
date.clear(); 
//Use Gregorian calendar for all values 
date.setGregorianChange(new Date(Long.MIN_VALUE)); 

date.setTimeZone(TimeZone.getTimeZone("UTC")); 
date.setTime(new Date(-62135596800000L)); 

System.out.println(
     date.get(GregorianCalendar.YEAR) + "-" + 
     (date.get(GregorianCalendar.MONTH) + 1) + "-" + 
     date.get(GregorianCalendar.DAY_OF_YEAR) + " " + 
     date.get(GregorianCalendar.HOUR_OF_DAY) + ":" + 
     date.get(GregorianCalendar.MINUTE) + ":" + 
     date.get(GregorianCalendar.SECOND) + "." + 
     date.get(GregorianCalendar.MILLISECOND) 
); 
//Prints 1-1-1 0:0:0.0 

不幸的是,我不知道如何進行格里高利從Calendar更改爲Date對象,所以我直接從日曆對象中進行格式設置。如果我剛剛做了 formatter.format(date.getTime())它將失去格里高利的變化設置並再次顯示第3天。

Julian日期提前2天,因爲根據this, 從公元前1年到公元100年,朱利安在格列高利時代前2天。


順便說一句,我建議使用JodaTime,它正確(我看來,though if you need something more convincing)默認爲純陽曆:

DateTime dt = new DateTime(-62135596800000L, DateTimeZone.UTC); 
System.out.println(dt.toString()); 
//0001-01-01T00:00:00.000Z 
+0

非常好......讓人難以置信! – ShrekOverflow