Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java getDate 0000/00/00 return strange value

Tags:

java

Why does this happens? For the month and day, I think Java is assuming the previous valid month and day, but I don't understand why year is 2.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
Date result = sdf.parse("0000/00/00");

System.out.println(result.toString());

Output is:

Sun Nov 30 00:00:00 GMT 2
like image 854
Gonçalo Cardoso Avatar asked May 22 '15 11:05

Gonçalo Cardoso


1 Answers

The Gregorian calendar does not have year 0.

Year 0 corresponds to 1BCE (Before Common Era, also known as BC).

Because you supply 0 for the month and 0 for the day, it rolls back to the previous month and previous year.

I.e. 30-Nov-0002 BCE.

Date#toString does not include BCE / CE suffix. It would be superfluous in the vast majority of cases.

If you are going to work with dates that far back then you need to consult with an historian.

like image 153
Bathsheba Avatar answered Sep 22 '22 20:09

Bathsheba