49

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

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
Tom
  • 586
  • 1
  • 4
  • 9

5 Answers5

117

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 3
    The context-param will not work prior to JSF 2.0, will it? An article about that: http://planet.jboss.org/post/step_right_up_and_select_your_time_zone – Zeemee Jul 06 '11 at 05:51
  • 2
    @Mulmoth: That's correct, but this should absolutely not form a problem for the OP as the question tags hints that s/he is already using JSF 2.0. – BalusC Jul 06 '11 at 05:58
  • You're right, I didn't see the tag. – Zeemee Jul 06 '11 at 07:13
  • Worked for me too . I am no OP but same issue and JSF2 . Can only +1 :( BalusC – Java Ka Baby Aug 25 '11 at 22:58
  • @Java: That's perfectly fine :) You're welcome. – BalusC Aug 25 '11 at 23:01
  • JSF defaults to UTC timezone for date/time converters is true for JSF2. But JSF 1.2 defaults to GMT (jsf-api-1.2_13.jar, DateTimeConverter.class:182). – Gabriel Feb 23 '17 at 09:05
17

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.

Zeemee
  • 10,486
  • 14
  • 51
  • 81
4

You are probably having issues with the Time zones.

Check this article on how to resolve them.

Before that, you can try explicitly setting the timeZone attribute of <f:convertDataTime>

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • I've tried to set the timeZone attribute of but now the time says 11 PM (because my timezone is GMT+1). It seems like is defaulting my time to 10PM UTC. – Tom Apr 22 '10 at 10:24
  • and did you try the other options? – Bozho Apr 25 '10 at 05:55
  • Yes. I've set the Timezone in an ServletContextListener but the result was (of course) the same. The core problem is that invents time values to my date-value (there is only a date, no time). However the workaround I currently use is to set the timezone to GMT+2, so that the output is 4/21/10 00:00:00 instead of 4/20/10 22:00:00. But thats not an ideal solution :) – Tom Apr 29 '10 at 06:07
  • well, if you store date and want to show time there is no way it can be relevant. – Bozho Apr 29 '10 at 06:20
  • yep and i just want to see a date(e.g. database field contains 4/22/10)! But then shows 4/21/10 (minus one day). So in order to investigate this problem i changed display-attribute to and voila pretends it's 22:00:00 in the evening...and that's the problem – Tom Apr 29 '10 at 14:01
0
<h:outputText id="dateId" value="#{item.date}">
 <f:convertDateTime for="dateId" type="date" dateStyle="long" locale="pl"/>   </h:outputText>
Tomasz
  • 56
  • 4
0

We had the same problem and ended up with writing a new JSF converter that uses a SimpleDateFormat created with new SimpleDateFormat(String pattern) for the date conversion.

silb
  • 46
  • 2