burntsugar
burntsugar

Reputation: 58030

Convert java.sql.Timestamp to local timezone

I am retrieving results from a timestamp mysql database column. I need to convert these results to my local timezone.Timestamp timestamp = rs.getTimestamp("mytimestamp");

Upvotes: 3

Views: 4364

Answers (3)

amicngh
amicngh

Reputation: 7899

java.sql.Timestampobjects don't have time zones - they are just like java.util.Date. However you can display time in your TimeZone like

 Calendar localTime = Calendar.getInstance(TimeZone.getDefault());
 localTime .setTimeInMillis(timeStamp.getTime());

Upvotes: 0

CodeChimp
CodeChimp

Reputation: 8154

I think this might work:

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timestamp.getTime());

I believe they are both based on epoch.

Upvotes: 1

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 135992

java.sql.Timestamp just like java.util.Date which it extends holds time since since January 1, 1970, 00:00:00 GMT with the only difference that it also holds nanos (see API). It does not depend on time zone and you cannot convert it to local time zone.

Upvotes: 1

Related Questions