Posts Tagged time zone

Clock change affecting date display

This is a very particular problem which happens only during the summer and not even in all the countries ! 😮
The problem is related to the Daylight Saving Time (DST), also called British Summer Time (BST):

Daylight saving time is the practice of temporarily advancing clocks so that afternoons have more daylight and mornings have less. Typically clocks are adjusted forward one hour near the start of spring and are adjusted backward in autumn.

Okay, so how do we display dates with JSF?
Let’s take this can’t-be-simpler bean:

public class MyBean {
	public Date getDate() {
		return new Date();
	}
}

And let’s insert the following code in a JSF page:

#{myBean.date}

The result is Fri Sep 17 20:03:14 BST 2010 (when I wrote this article).
If I check my clock, the date and time above are correct.
So far so good. 🙂

Let’s now use the h:outputText tag from JSF:

<h:outputText value="#{myBean.date}"/>

The result is Sep 17, 2010.
Alright, the date is correct but the time is not displayed…

Let’s use the f:convertDateTime tag to also display the time:

<h:outputText value="#{myBean.date}">
	<f:convertDateTime pattern="E MMM dd HH:mm:ss z yyyy" />
</h:outputText>

The result is Fri Sep 17 19:03:14 GMT 2010.
What can we see here? The time went one hour backward!
Why that? well, simply because the date is now displayed in GMT, instead of BST earlier.

So, this means that the h:outputText tag is displaying the date in GMT by default, which would be fine in winter but not in summer.
In order to fix this behaviour, you need to add the attribute timeZone to the f:convertDateTime tag such as:

<h:outputText value="#{myBean.date}">
	<f:convertDateTime pattern="E MMM dd HH:mm:ss z yyyy" timeZone="GB" />
</h:outputText>

Note that if you put BST instead of GB in the timeZone attribute, the time zone is going to be actually set to BDST which stands for Bangladesh Daylight Saving Time! 😀

, , , , ,

No Comments