Timestamp show wrong data

僤鯓⒐⒋嵵緔 提交于 2020-01-02 04:59:06

问题


Why Timestamp((long)-62135596800000L) return 0001-01-03 00:00:00, but must return 0001-01-01 00:00:00? This service show correct Timestamp here


回答1:


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

The website you are using, uses javascript which uses extrapolated, or proleptic, Gregorian calendar for all dates. From javascript specification

ECMAScript uses an extrapolated Gregorian system to map a day number to a year number and to determine the month and date within that year.

Indeed, in javascript:

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

You can use something like this in java to gain the same results:

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

Unfortunately I don't know how to carry out the Gregorian change from Calendar to Date objects, so I am doing the formatting directly from the calendar object. If I just did formatter.format(date.getTime()) it would lose the Gregorian change setting and show 3rd day again.

The Julian date is 2 days ahead because according to this, Julian is ahead of proleptic Gregorian by 2 days from 1 BC to 100 AD.


Btw, I recommend using JodaTime, it correctly (my opinion, though if you need something more convincing) defaults to pure Gregorian:

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


来源:https://stackoverflow.com/questions/13668603/timestamp-show-wrong-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!