Search code examples
javajava-8java-timesqldatetime

Which timezone is java.sql.Date toLocalDate using


I'm surprised that java.sql.Date has a method toLocalDate().

java.util.Date or java.time.Instant don't have comparable methods. It seems that in java.time, a ZoneId must always be provided to obtain "LocalFoo" or "OffsetBar".

From the javadoc of java.sql.Date#toLocalDate():

Converts this Date object to a LocalDate. The conversion creates a LocalDate that represents the same date value as this Date in local time zone

Which timezone is "local time zone"? Does it depend on database or JVM settings?


Solution

  • given a java.sql.Date-Object wrapping a fix millisecondsvalue, you will indeed get different LocalDate-Values depending on the Default-Timezone on your System.

    I tried the following Code-Snippet:

    TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
    java.sql.Date sqlDate = new java.sql.Date(1465485798671l);
    System.out.println(sqlDate.toLocalDate());
    TimeZone.setDefault(TimeZone.getTimeZone("Asia/Tokyo"));
    System.out.println(sqlDate.toLocalDate());
    

    which yields: 2016-06-09 2016-06-10