2

It seems that Java and JavaScript give different results for negative millisecond values.

JAVA:

System.out.println(new Date(-12220000000000L));
Wed Sep 26 12:33:20 MST 1582

JavaScript:

console.log(new Date(-12220000000000));
 Date {Wed Oct 06 1582 12:33:20 GMT-0700 (LMT)}

There is a 10 day difference in their outputs. But for some values, the difference is less than 10 days. I used W3C TryIt editor to test JavaScript output. I don't know exactly where the deviation begins. Am I doing something wrong here?

jrook
  • 3,459
  • 1
  • 16
  • 33
  • Why would `new Date` accept something like that ? Do you have any documentation that shows the Date constructor accepting negative numbers. – adeneo Mar 21 '15 at 22:17
  • Technically the Java API docs says that negative integers are allowed. Don't know about the JavaScript api. – Florian Schaetz Mar 21 '15 at 22:25
  • In what locale? I note the first is in "MST" and the second is in "LMT" – Elliott Frisch Mar 21 '15 at 22:32
  • 1
    @adeneo:Negative numbers are valid arguments for Date constructor both in Java and JavaScript. In any case, there is no difference between the two as long as absolute values of the numbers are small enough. I haven't tried to find the exact number a deviation occurs between the two. – jrook Mar 21 '15 at 22:50
  • @Elliott Frisch: LMT stands for Local Mean Time. Actually, MST=GMT-07. Anyway, this can't be a timezone issue. There is 10 day difference. No two time zones are that much apart. – jrook Mar 21 '15 at 22:50
  • Here's a little conundrum to make you confused -> **http://jsfiddle.net/svu9L6ys/** – adeneo Mar 21 '15 at 23:05
  • The deviation begins at -12219292800000. Java goes from Oct 4 to Oct 15, and Javascript goes from Oct 14 to Oct 15. – fgb Mar 21 '15 at 23:51
  • for -30000000000000 java gives: Tue Apr 28 1019, and JavaScript gives: Tue May 04 1019. It seems the difference is not always fixed. For -50000000000000, the difference becomes only 1 day! – jrook Mar 22 '15 at 00:02
  • @jrook See [Conversion between Julian and Gregorian calendars #Conversion table](http://en.wikipedia.org/wiki/Conversion_between_Julian_and_Gregorian_calendars#Conversion_table). – Gerold Broser Mar 22 '15 at 00:42

3 Answers3

3

The default date for the switch from Julian to Gregorian calendar in Java's GregorianCalendar class "is October 15, 1582 (Gregorian). Previous to this, dates will be in the Julian calendar."

Hence, the OP's Java date of Sep 26, 1582 is a Julian date. The difference between Julian and Gregorian dates in 1582 was 10 days.

If the JavaScript implementation in question doesn't respect the Julian to Gregorian switch but works with Gregorian dates regardless of the point in time we are at the situation the OP experienced.

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
1

Using nashorn, you can see when they deviate:

> function printDate(x) { System.out.println(new Date(x) + " - " + new java.util.Date(x)); }

> printDate(-12219292800000)
Fri Oct 15 1582 00:00:00 GMT+0000 (GMT) - Fri Oct 15 00:00:00 GMT 1582

> printDate(-12219292800001)
Thu Oct 14 1582 23:59:59 GMT+0000 (GMT) - Thu Oct 04 23:59:59 GMT 1582

This corresponds to a switch from the Julian to Gregorian calendar.

Java's date library takes this change into account, but JavaScript's doesn't.

fgb
  • 18,439
  • 2
  • 38
  • 52
  • Is there a way to resolve this? Because the difference doesn't seem to be fixed and seems to be decreasing as the absolute value of the Date argument increases. – jrook Mar 22 '15 at 00:19
  • @jrook You can try the `GregorianCalendar` class in Java instead of `Date`. – fgb Mar 22 '15 at 00:24
  • or check Jodatime or the Java 8 Date api – fgb Mar 22 '15 at 00:26
  • @jrook Use [GregorianCalendar.setGregorianChange(...)](http://docs.oracle.com/javase/8/docs/api/java/util/GregorianCalendar.html#setGregorianChange-java.util.Date-) and set it to a date prior to the range you are working with. – Gerold Broser Mar 22 '15 at 00:32
0

Both java getTime() and javascript getTime() returns many milliseconds have passed since January 1, 1970, 00:00:00 GMT. Negative argument calculate the date to the past.

I found a bug in jdk . Reading this it seems in jdk 7 there is a lack of 10 days from Oct 5 1582 to Oct 15 1582. I think the 10 days difference from javascript getTime() comes from this bug.

Andy
  • 49,085
  • 60
  • 166
  • 233
Razib
  • 10,965
  • 11
  • 53
  • 80
  • It's not a difference of 10 days, it's 2 days: _"JRE6 jumps from Oct 5 1582 to **Oct 17 1582**"_ vs. _"JRE7 jumps from Oct 5 1582 to **Oct 15 1582**"_ – Gerold Broser Mar 21 '15 at 23:07
  • It's a difference of 10 days when comparing JRE7 to JavaScript. – fgb Mar 21 '15 at 23:52
  • @fgb Yes, that's what the OP found out already. The JDK bug report doesn't mention JavaScript but a 2-days difference between JRE6 and JRE7. – Gerold Broser Mar 22 '15 at 00:08