1

RE-EDITED

I have a bean with a field "lastUpdate", which is of type Date, and its usual getter/setter. I use JPA to persist my beans into a PostGreSql DB.

I want to have my lastUpdate be saved in UTC, so before setting it, I set the time zone:

Calendar calendar = DatatypeConverter.parseDateTime( text.toString() );
calendar.setTimeZone( TimeZone.getTimeZone( "Europe/Zurich" ) );
doiPool.setLastUpdate( calendar.getTime() );

So, if lastUpdate is "2013-08-02 12:17:05" (Europe/Zurich), in the DB I have "2013-08-02 10:17:05" (UTC), which is what I want.

When I display the date back, I also want to have it according to my time zone, so I thought I could use the f:convertDateTime and setting the timeZone="xxx".

...
<f:facet name="header"> #{msg.dashboardColumnLastUpdate} </f:facet>
<h:outputText value="#{dashboardDoiPoolRow.doiPool.lastUpdate}">
       <f:convertDateTime pattern="yyyy-MM-dd HH:mm" timeZone="Europe/Zurich"/>
</h:outputText>
...

Reading other answers, I tried to set the

javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE

too, but the result is always the same: by displaying the date I get 2013-08-02 10:17:05 instead 2013-08-02 12:17:05.

I think that if the converter does its job correctly, if I want to display 2013-08-02 10:17:05 (UTC) according my time zone, setting the time zone to mine (Europe/Zurich), I should get 2013-08-02 12:17:05

Do I miss something??

In the DB, the column is of type "timestamp without time zone". Does it have some effect?

Francesco
  • 2,350
  • 11
  • 36
  • 59
  • I'm not sure how your `setLastUpdate()` makes ever sense. The result is **always** the same as the input. Isn't the timezone of the DB itself just plain wrong? – BalusC Aug 02 '13 at 17:18
  • Hi BalusC, I edited my question fixing the code of the set method. But as said I still get the wrong time... – Francesco Aug 05 '13 at 10:35

2 Answers2

0

I always use Greenwich Mean Time as reference to set it. Departing from here, you just add or deduct ours to fix it to your current timezone. That would be a workaround for you:

<f:convertDateTime pattern="yyyy-MM-dd HH:mm" timeZone="GMT-2"/>

If you want to avoid having to specify it once and again you can create your own facelet tag. That way you can reuse code and you'll have to change it only once in case of timezone change.

<mytag:customDate value="#{bean.date}" />
Community
  • 1
  • 1
Aritz
  • 30,971
  • 16
  • 136
  • 217
0

In the DB, the column is of type "timestamp without time zone".

This is the problem. If no time zone is specified, how can the converter know how to adjust the time?

Francesco
  • 2,350
  • 11
  • 36
  • 59