The accepted answer is not perfect, so I decided to add my 2 cents
timeStamp.toLocalDateTime().toLocalDate();
this is a bad decision in general , I'm not even sure why they added this method to the JDK, since it makes things really confusing, making an implicit conversion using the system time zone. Usually, when only java8 date classes are used, the programmer is forced to specify the time zone, which is good.
Good decision -
timestamp.toInstant().atZone(zoneId).toLocalDate()
Where zoneId is the time zone you want to use. This is usually ZoneId.systemDefault () if you want to use the system time zone or some hard-coded time zone, such as ZoneOffset..UTC
The general approach should be
- Get rid of the new java8 date classes using a class that has a direct relationship, for example, in our case, java.time.Instant is directly related to java.sql.Timestamp , i.e. Converting time zones between them is not required.
- Use properly designed methods in this java8 class to do the right thing. In our case, atZone (zoneId) directly indicated that we are doing the conversion and use a specific time zone for it.
Ruslan Jul 18 '19 at 19:35 2019-07-18 19:35
source share