Strange result with negative parameter with Date

Viewed 112

Consider the following program

    Date date1 = new Date(-124304227239000L);
    Date date2 = new Date(0);
    System.out.println(date1 + " vs. " + date2);

The result (at least with Java 8 on my computer, and with Java 11 on a different computer):

Sun Jan 01 16:59:21 CET 1970 vs. Thu Jan 01 01:00:00 CET 1970

This seems strange because following the documentation (https://docs.oracle.com/javase/8/docs/api/java/util/Date.html#Date-long-) , negative values as parameter for Date indicate dates before 1970. Instead, I get a Sunday instead of Thursday, but still 1970.

Can anybody explain this to me?

1 Answers

The value you've provided is around 1969/1970 BC, depending on whether you do a Gregorian/Julian cutover or not. Date.toString(), aside from all its other problems, doesn't bother to mention the era.

If you use Instant with the same value, it's clearer:

Instant instant = Instant.ofEpochMilli(-124304227239000L);
System.out.println(instant);

Output:

-1970-12-15T15:59:21Z

I'd draw the following conclusions from this:

  • When using values in the far past, there are lots of considerations to bear in mind, including textual representation and calendar system
  • Avoid java.util.Date as far as you can
Related