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