Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i fetch date timestamp as UTC

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.

like image 899
M S Parmar Avatar asked May 19 '15 07:05

M S Parmar


1 Answers

Java 8

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

Joda-Time

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());
like image 123
MadProgrammer Avatar answered Oct 10 '22 14:10

MadProgrammer