39

I use JSF1.2 and I have a little problem with time zones.

Calendar respects my timezone and save the correct time in the Database. When I show it using a h:outputtext with a f:convertDateTime it shows the wrong date (I think with default time zone).

I can do something like:

<h:outputText value="#{atividade.atividade.dataCriacao.time}">
  <f:convertDateTime pattern="#{msg.formatoDataCalendario2}" timeZone="America/Sao_Paulo" />
</h:outputText>

formatoDataCalendario2 = dd/MM/yyyy, HH:mm in messages.properties.

I can put the time zone in messages too, but I believe it have some configuration that I could use.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
caarlos0
  • 20,020
  • 27
  • 85
  • 160

3 Answers3

97

JSF date/time converters defaults by specification to UTC timezone. If you want to use a different timezone, then you really need to specify it in the converter yourself. Or, if you have 100% control over the production runtime environment, then since JSF 2.0 you can set its system timezone to the desired timezone and add 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>

This way JSF will use the system's timezone as obtained by TimeZone#getDefault() as converter's default timezone.

Please note that the java.util.Date object by itself also does not store any timezone information. It also always defaults to UTC timezone. Keep this in mind when processing submitted date/times.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • that work, but, if I want to force some timezone, America/Sao_Paulo, for example? – caarlos0 Sep 21 '11 at 20:16
  • There are no other easy ways than mentioned in my answer. You could however create a custom tag which extends/wraps the `f:dataTimeConverter` or create a custom converter. – BalusC Sep 21 '11 at 20:21
  • 6
    Caarlos wrote that he is using JSF 1.2. As far as I know javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE works since JSF 2.0. Is this solution really valid for JSF 1.2? Thanks @BalusC. – Ondrej Bozek Feb 27 '12 at 15:36
  • 4
    @Infragile: Hmm, I must have overlooked this statement in the question :o It's indeed JSF 2.x specific. For JSF 1.x you'd need to explicitly specify `timeZone` attribute in every single `` tag, or to extend the `DateTimeConverter` class and use it instead as ``. – BalusC Feb 27 '12 at 15:54
  • Why oh Why? What possible use could there be for the default time zone to be UTC? Thanks for the answer. – Martlark Jan 23 '15 at 00:39
1

I had scenario like this. In my JSF app I was using as @BalusC pointed out:

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

, and I had in xhtml:

<f:convertDateTime pattern="dd MMM yyyy - HH:mm:ss" type="date" />

However, when I receive date/time like 2020-02-18T10:15:20, That would be converted into GMT time (- 8 hours for me in PST zone). So, it would end up being 2020-02-18T02:15:20. That is because convertDateTime treats dates/times without time zone offset as GMT time by default so it converted it to GMT time.

Once I started receiving offset as well like 2020-02-18T10:15:20-07:00, my date/time would not longer be converted to GMT since it would recognize that the offset is matching the PST zone offset and would therefore do no conversion to GMT.

Hope that helps little bit.

pixel
  • 9,653
  • 16
  • 82
  • 149
-5

For displaying date correctly you need to modify only in your web.xml

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

and in your xhtml file

<h:outputText value="#{report.date}">
    <f:convertDateTime pattern="dd-MMM-yyyy" />
</h:outputText>

Its a timezone issue. The context param should fix it.

amit
  • 74
  • 6
  • 21
    Why are you repeating an already given answer? This is a Q&A site, not an old fashioned discussion forum where everyone repeats each other on confirmation. Just upvote the post if you agree it. – BalusC Jan 02 '13 at 16:19