java – How to convert Java.sql.timestamp to LocalDate (Java8) Java.time?

The accepted answer is not ideal, so I decided to add my 2 cents


it’s a bad solution in general, I’m not even sure why they added this method to the JDK as it makes things really confusing by doing an implicit conversion using the system’s time zone. Usually when using only Java8 date classes, the programmer is forced to specify a time zone which is a good thing.

The good solution is


Where is it zoneId is the time zone you want to use, generally ZoneId.systemDefault () if you want to use the system time zone or a programmed time zone such as ZoneOffset .UTC

The general approach should be

  1. Break free from the new Java8 date classes by using a directly related class, e.g. I our case Java.time.Instant is directly related to Java.sql.Timestampthat is, no time zone conversion is required between them.
  2. Use the well-designed methods in this Java8 class to do the right thing. I our case atZone (zoneId) made it explicit that we are doing a conversion and are using a particular time zone for it.

Leave a comment