The accepted answer is not ideal, so I decided to add my 2 cents
timeStamp.toLocalDateTime().toLocalDate();
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
timestamp.toInstant().atZone(zoneId).toLocalDate()
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
- 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.
- 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.