Monday, 21 August 2017

Convert java.sql.Timestamp to Java 8 ZonedDateTime?



Migrating Joda time to Java 8



Joda:



UserObject user = new UserObject()
user.setCreatedAt(new DateTime(rs.getTimestamp("columnName")));`



Migrating to Java 8



This is my code; it does compile; I am doubtful if it works:



ZonedDateTime.ofInstant(rs.getTimestamp("columnName").toLocalDateTime().toInstant(ZoneOffset.UTC),ZoneId.of("UTC")));


In some cases, the date is wrong. Any advice?


Answer



This seems to work:-




ZonedDateTime.ofInstant(rs.getTimestamp("columnname").toInstant(), ZoneId.of("UTC"))

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...