Jsf Convertdatetime Renders the Previous Day

JSF convertDateTime renders the previous day

This is undoubtedly a timezone-related issue.

JSF defaults to GMT (UTC) in date/time conversion. So if your server platform default timezone is GMT+X (not GMT-X), then the time will go back in the past X-amount of hours. If the time is already 00:00:00 (midnight), then the date will even go back one day in the past.

There are 2 standard ways to achieve your functional requirement anyway:

  1. Tell JSF to use the server platform default timezone instead for all date/time conversion by adding the following context parameter to web.xml:

    <context-param>
    <param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name>
    <param-value>true</param-value>
    </context-param>
  2. Alter every <f:convertDateTime> to explicitly specify the webapp-specific timezone. As you're based in Germany and the date format pattern also confirms this, I'll assume CET.

    <f:convertDateTime ... timeZone="CET" />

In any case, using a non-universal or even mixed timezone throughout the application is not recommendable. It's recommend to set the timezone in all layers and environments to UTC. Not only in the server and front end tier and presentation layer, but also in the SQL database and back end tier and persistence layer. This way the code is not sensitive to timezone and DST(!) related matters and you can just focus on altering the timezone during presentation only, if necessary.

See also:

  • Daylight saving time and time zone best practices

JSF page reflects Date incorrectly - 1 Day shifted

The JSF date converters defaults to UTC timezone. But your date is apparently stored using EEST timezone which is some hours beyond UTC (GMT+3 to be precise). When intepreting those dates using UTC timezone (as JSF by default does), you will get hours back in time and thus the previous day will be represented.

You need to explicitly specify the timezone in <f:convertDateTime>:

<f:convertDateTime pattern="dd.MM.yyyy" timeZone="GMT+3" />

24h problems in convertDateTime in JSF

The <f:convertDateTime> uses java.text.SimpleDateFormat API under the covers, whose javadoc is available here, listing all available pattern letters.

For hours, you used a pattern of hh which is in the javadoc described as


h Hour in am/pm (1-12)

, while you clearly want the one described as follows in the javadoc:


H Hour in day (0-23)

Fix it accordingly:

<f:convertDateTime type="time" pattern="HH:mm" timeZone="#{backBean.timeZone}"/>

Convert Date Time converting weird time

i guess JSF date/time converters defaults by specification to UTC timezone.

so i added this on my web.xml and it worked.

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

reference:
Set a default time zone for f:convertDateTime

Validating Persistent Date in JSF entity

The <f:convertDateTime> isn't a validator, but a converter. The JSR303 Bean Validation API doesn't offer converters, only validators. That's why it can't be supplanted by some JSR303 annotation.

As to time zone, JSF relies by default on GMT. If you make sure the rest of your environment is also relying on GMT, then you're safe. If you however rely on a different time zone, then you can tell JSF that on either an application-wide basis or on a converter-specific basis. Detail can be found in this answer: JSF convertDateTime renders the previous day.



Related Topics



Leave a reply



Submit