I'm working with webservices, inserting records, that return a timestamp value in XMLGregorianCalendar type. I need to transform it in a java.sql.Timestamp value, so I use a function like this.
public static java.sql.Timestamp getSqlTimeStamp(XMLGregorianCalendar xgc) {
if (xgc == null) {
return null;
} else {
return new Timestamp(xgc.toGregorianCalendar().getTime().getTime());
}
}
Timestamp timestamp=getSqlTimeStamp(ExitInXMLGregor.getTimestamp());
My problem is that in the server, the timestamp value when I insert a record, looks like this: 2012-10-03T19:23:22.342+02:00
But when I make my type conversion, I obtain the timestamp value like this: 2012-10-03T17:23:22.342
The time in the server (where the webservice is located) is 2h more than my locale, and for some reason, I obtain my insert locale time, after transform it. The problem is that I really need to obtain the server time, cause in the DB, the timestamp value matches with the server one, and I'm having problems in the update operation, due to the different values of timestamp.
Please, I would appreciate any kind of help. thanks!
Edit: I kind of find a solution, but is not exactly what I need. When I convert my timestamp in java.sql.Timestamp format into XMLGregorian I setting the timeZone of the server (setTimeZone(TimeZone.getTimeZone("GMT+02:00"))). This actually works, but is far away from the ideal solution (It could happen that the timezone or even the server change) It would be great to know in this point the timeZone of the server dinamically, but I don't know how...
public static XMLGregorianCalendar getXMLGregorianCalendar(Timestamp timestamp)
throws BaseException {
try {
GregorianCalendar gc = new GregorianCalendar();
gc.setTimeZone(TimeZone.getTimeZone("GMT+02:00"));
gc.setTimeInMillis(timestamp.getTime());
return DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
} catch (DatatypeConfigurationException ex) {
throw new BaseException(ex);
}
}