0

I have a date in represented by the string "0002-01-04T00:49:40.000" i.e., a date in the year 2CE. I need to convert it to an instance ofjava.util.Date by combining it with the timezone id "Etc/UTC". The following code shows how I do it:

public static Date toDate(LocalDateTime localDateTime, String timezoneId){
    if(localDateTime == null) return null;
    if(timezoneId != null) {
        localDateTime.toDateTime(DateTimeZone.forID(timezoneId)).toDate();
    } else {
        return localDateTime.toDateTime().toDate()
    }
}

But LocalDateTime.toDate() doesn't work correctly. It adds +1 day to date.

"0002-01-04T00:49:40.000Z" ---> "Thu Jan 05 16:49:40 PST 2".

Guillaume Jacquenot
  • 11,217
  • 6
  • 43
  • 49
  • 4
    Possible duplicate of [How to convert String of datetime to date using GWT?](http://stackoverflow.com/questions/11342670/how-to-convert-string-of-datetime-to-date-using-gwt) – DimaSan Nov 29 '16 at 21:17
  • `toDate()` is [JodaTime](http://www.joda.org/joda-time/)'s method. – DimaSan Nov 29 '16 at 21:19
  • `java.util.Date.toString()` uses the Julian calendar for the year 0002! So I see no problem or failure. – Meno Hochschild Nov 29 '16 at 21:38
  • 1
    @DimaSan The link you posted does not tackle the specific problem of OP regarding the observed output, see my answer. – Meno Hochschild Nov 30 '16 at 12:14

1 Answers1

3

As indicated in my comment, there is NO error. All is correct although you didn't expect the result you observed. Therefore I will explain it in detail. Example code based on your helper method:

System.out.println(
    toDate(org.joda.time.LocalDateTime.parse("0002-01-04T00:49:40.000"), "GMT")
); // Fri Jan 06 01:49:40 CET 2

The output is based on the method java.util.Date.toString(). The confusing thing about this method is:

  • It uses the system time zone (in my case GMT+01:00). Hence this detail explains why - in my case - the time is one hour ahead (and in your case 8 hours behind UTC due to your zone PST). If we only consider the time then we would expect as date the same day for GMT+01 and one day earlier for PST...

  • Less known but very important: The output of java.util.Date.toString() uses a mixed calendar with gregorian cutover set to 1582-10-15. Before this date, the Julian calendar is used. And you use the year 0002! So here we compare the proleptic gregorian date part of Joda-Time with a Julian calendar date. For the year 0002, there are two days difference between both calendars due to different leap year rules. The Julian calendar is two days ahead in year 0002. How to understand these two days? The years 100, 200, 300, 500, 600, 700, 900, 1000, 1100, 1300, 1400 and 1500 are no leap years in gregorian calendar while leap years in Julian calendar => 12 days difference. But the pope Gregor had deleted 10 days in 1582 (next day after 1582-10-04 was 1582-10-15). Hence: 12 - 10 = 2 days delta for the years before 100 AD and after 99 BC.

  • Summarizing: [0002-01-04] minus one day (time correction for PST) plus two days (calendar correction) results in [0002-01-05] what you observe.

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126