0

I have a field in my db name as updation_date and i save data in it as datetime/ timestamp and i am saving UTC datetime. Now when i retrieve the long values i do this

rs.getTimestamp("UPDATION_DATE").getTime()

Now when i run the code on my local machine i get the right UTC time (the same one i saved in db) but when i run the same code on my server (it's in Netharlands) i get long value but having 3 hours extra then UTC time. I don't know why i get the difference because i just converting the time i get from db to long.

Updation: I tried using this function to get UTC date but still no effect

private static String DATEFORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";

public static Long getUTCTime(Timestamp dateTime) throws ParseException{
    SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT);
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

    Date date = sdf.parse(getStringDate(dateTime));
    return date.getTime();
}

public static String getStringDate(Timestamp dateTime) throws ParseException
{
    SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT);        
    return sdf.format(new Date(dateTime.getTime()));
}

and now save long like this

setUpdationDate(Constants.getUTCTime(rs.getTimestamp("UPDATION_DATE"));
Syed Muhammad Oan
  • 687
  • 2
  • 15
  • 39

1 Answers1

0

From Javadoc:

Sets the designated parameter to the given java.sql.Timestamp value, using the given Calendar object. The driver uses the Calendar object to construct an SQL TIMESTAMP value, which the driver then sends to the database. With a Calendar object, the driver can calculate the timestamp taking into account a custom time zone. If no Calendar object is specified, the driver uses the default time zone, which is that of the virtual machine running the application.

Is java.sql.Timestamp timezone specific?

Community
  • 1
  • 1
ugurdonmez
  • 154
  • 1
  • 9
  • why do i get the correct utc time when i run this locally , i mean obviously my timezone is also different i.e GMT – Syed Muhammad Oan Apr 19 '16 at 08:26
  • do your local have 3 hours difference between Netherlands, the situation is normal. Try to get time with specific time zone, probably you will get different answer. If you do not specify time zone when you save the date, you can not sure that you get UTC time in your local. – ugurdonmez Apr 19 '16 at 08:31
  • seem you live in Islamabad (GMT+5) and your server is in Netherlands (GMT+2). So it is normal that you get 3 hours difference when you convert long millisecond to datetime. – ugurdonmez Apr 19 '16 at 08:36
  • i have updated my question and trying something new but still no effect . What should i do – Syed Muhammad Oan Apr 19 '16 at 09:24