I am storing date timestamps in DB as UTC value, while retrieving it back i need to make as UTC time and need to convert to specific timezone value.
i.e. 2015-05-01 00:09:30:00 UTC time need to convert to IST(Or other timezone)
resultSet.getDate("VisitDate")
please help on this.
You can use a ZonedDateTime
set to UTC and then translate it to a LocalDateTime
using something like...
java.sql.Timestamp ts = resultSet.getTimestamp("VisitDate");
ZonedDateTime utcDateTime = ZonedDateTime.ofInstant(ts.toInstant(), ZoneId.of("UTC"));
LocalDateTime localDateTime = utcDateTime.withZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime();
Obviously, I've use ZoneId.systemDefault()
in the example (convert the zoned date/time to local date/time), but you can pass what ever zone you want/need
And similarly with Joda-Time if you're not using Java 8
java.sql.Timestamp ts = resultSet.getTimestamp("VisitDate");
LocalDateTime utcDateTime = new LocalDateTime(ts, DateTimeZone.UTC);
DateTime hereDateTime = utcDateTime.toDateTime(DateTimeZone.getDefault());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With