3

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");

burntsugar
  • 57,360
  • 21
  • 58
  • 81

3 Answers3

1

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.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
1

I think this might work:

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

I believe they are both based on epoch.

CodeChimp
  • 8,016
  • 5
  • 41
  • 79
0

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());
amicngh
  • 7,831
  • 3
  • 35
  • 54
  • Unfortunately Timestamp does have zones (inherited from java.util.Date) depending on which constructor is used. Some JDBC drivers still use the deprecated constructor in which case the local timezone is selected. See http://www.docjar.com/html/api/java/util/Date.java.html (line 252) – Jonathan Apr 02 '14 at 16:48
  • @Jonathan , neither Date nor Timestamp has time zones though they are related to UTC in a way. Any hint in the javadoc that they have time zones was deprecated in Java 1.1 back in early 1997. Your comment refers to such a deprecated method. Notice that there is no getTimeZone method in Date as there is in Calendar. – BPS Sep 10 '15 at 19:46