Reputation: 1011
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?
Upvotes: 3
Views: 2870
Reputation: 63355
Conceptually, a java.sql.Date
is the same as a LocalDate
, ie. a date without time or time-zone. In practice, because java.sql.Date
is implemented based on a long
milliseconds, it has an implicit use of the system time-zone. So, the toLocalDate()
method exists because the Java SQL spec leads want you to think of java.sql.Date
as being without a time-zone.
Upvotes: 5
Reputation: 36
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
Upvotes: 2