Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timestamp show wrong data

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

enter image description here

like image 320
Igor Kostenko Avatar asked Dec 02 '12 10:12

Igor Kostenko


1 Answers

-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
like image 190
Esailija Avatar answered Nov 01 '22 18:11

Esailija