This sounds like a simple task.
Get UTC timestamp value from DB and pass it as UTC date via Web Service.
We have timestamp column DATE_COLUMN and store there time in UTC time zone.
With JPA we get this time with
@Column(name = "DATE_COLUMN")
private java.sql.Timestamp dateValue;
And as we have to pass this time via Web Service in UTC (Jax-ws 2.0) we have getDate and setDate methods.
We are interested in getDate.
public Calendar getDate()
{
Calendar calendar = Calendar.getInstance(utcTimeZone);
calendar.setTimeInMillis(dateValue.getTime());
return calendar;
}
This doesn't work as you may think it should.
And this is because application's default time zone is not 'UTC'.
Here is an example for clarification.
Value in the DATE_COLUMN equals to "30.11.09 16:34:48,833045000", when I translate it to UTC I get "2009-11-30T14:34:48.833Z".
The difference is 2 hours. And this is because my default time zone is "Europe/Helsinki".
Same problem if you just want to map 'DATE_COLUMN' to Calendar
@Column(name = "DATE_COLUMN")
@Temporal(TemporalType.TIMESTAMP)
private Calendar dateValue;
public Calendar getDate()
{
calendar.setTimeZone(utcTimeZone);
return calendar;
}
I don't want to change application's time zone because it doesn't look like the solution.
By now we have only two options.
First. Calculate offset between application's time zone and UTC and add it manually after automatic subtraction in the calendar.setTimeZone.
public Calendar getDate()
{
Calendar calendar = Calendar.getInstance(utcTimeZone);
calendar.setTimeInMillis(dateValue.getTime());
int offset = TimeZone.getDefault().getOffset(dateValue.getTime());
calendar.add(Calendar.MILLISECOND, offset);
return calendar;
}
Second. Pass dateValue as Long
via Web Service. Which is not bad except that we lose real type of the field in wsdl.
My imaginary solution is
@Column(name = "DATE_COLUMN")
@Temporal(type = TemporalType.TIMESTAMP, timezone = 'UTC')
private Calendar dateValue;
But I tend to think that there is the real one somewhere. And I hope you can point it out.