Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

f:convertDateTime displays wrong Date [duplicate]

In my Web Application I retrieve data using Hibernate and display it in a RichFaces dataTable.

In my MySQL-table there is a field of type "date". When I print this field to the log in my Bean, it shows the correct date from database (e.g. 2010-04-21). But in the rich:dataTable it shows up like this:

4/20/10

So there is a discrepancy of 1 day!

I added the "f:convertDateTime" converter and set the "type" attribute to "both" in order to display time too. So now it shows:

4/20/10 10:00:00 PM

The Code for "f:convertDateTime" I've used:

<f:convertDateTime locale="locale.US" type="both" dateStyle="short"/> 

So it seems like f:convertDateTime dreams up some time because there is no time information in the MySQL-table field!

What am I doing wrong? What do I need to do to display the correct date?

Thanks Tom

like image 880
Tom Avatar asked Apr 22 '10 08:04

Tom


2 Answers

JSF defaults to UTC timezone for date/time converters. To override this you need to set the timeZone attribute in every date/time converter. Here's an example using EDT timezone (assuming you're on the east of US).

<f:convertDateTime locale="en_US" type="both" dateStyle="short" timeZone="EDT" /> 

The locale attribute only controls the full day/month name formatting (it becomes English).

If you want to override the default UTC timezone to be the operating platform default timezone, then you need to add the following context param to web.xml:

<context-param>     <param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name>     <param-value>true</param-value> </context-param> 

Then you don't need to edit every individual JSF <f:convertXxx> tag.

like image 110
BalusC Avatar answered Sep 18 '22 14:09

BalusC


According to the JSF specs, f:convertDateTime defaults to UTC timezone (regardless of any VM timezone setting), which differs from your timezone by -1 hour (standard time) or -2 hours (summer time).

We use a application scoped page bean with an timeZone property like this:

public TimeZone getTimeZone() {     return TimeZone.getDefault(); } 

Then we use the property in an EL expression:

<ice:outputText value="#{deliveryDate}">     <f:convertDateTime type="both" timeZone="#{Application.timeZone}" /> </ice:outputText> 

The advantage is that it's considering standard/summer time automatically.

like image 35
Zeemee Avatar answered Sep 19 '22 14:09

Zeemee