0

I have a java.sql.Timestamp variable in local timezone with following formatting

2011-07-21 00:40:37

I need to convert it to UTC

I asked this question to see if it can be done by mysql, but received no answers.

I guess I have to somehow do it in java code before substituting it in the query

Community
  • 1
  • 1
storm_to
  • 1,495
  • 2
  • 16
  • 24
  • http://stackoverflow.com/questions/2609360/converting-local-timestamp-to-utc-timestamp-in-java Already asked. – Jon Martin Jul 25 '11 at 20:59
  • @Jon That won't work for me because it doesn't take into account the variable DST. This code will be deployed in multiple timezones and I will need it to be accurate in UTC conversion based on its location and time of year... – storm_to Jul 25 '11 at 21:03
  • MySQL solution `SELECT CONVERT_TZ( NOW(), @@session.time_zone, '+00:00' )` – storm_to Feb 14 '12 at 16:54

1 Answers1

2

I recommend using Joda Time to parse the timestamp. You'll need to set the timezone to EST and then get the UTC time from there. You could do the same thing with the generic Java Date, but Joda is easier to work with.

plor
  • 282
  • 2
  • 12
  • Is there a way to read local timezone? EST was just an example, but it could be any timezone – storm_to Jul 25 '11 at 21:05
  • After a closer look at Joda time I see it has `public long convertLocalToUTC(long instantLocal, boolean strict)` Looks like setting the strict argument to True will make it account for DST if applicable. Hopefully it will turn out to be exactly what I need. Won't know for sure until after QA's done – storm_to Jul 26 '11 at 14:49