JSF default timezone and date pattern

By default JSF using GMT timezone to convert the date into display format. I did Google search about this issue, found no simple solution to solve this problem.

Here is a simple solution where you can provide your custom date time converter. I like this solution because it fixes GMT date/time issue across the application.

1) Write a new custom converter by extending existing DateTimeConverter class.

package com.company.project.converter;

import java.util.TimeZone;

import javax.faces.convert.DateTimeConverter;

public class CustomDateTimeConverter extends DateTimeConverter {

public CustomDateTimeConverter() {
super();
setTimeZone(TimeZone.getDefault());
// here you can set your custom date pattern for your project
// setPattern("M/d/yy");
}

}

Bind your new converter as default converter for object of type java.util.Date

<converter>
<converter-for-class>java.util.Date</converter-for-class>
<converter-class>com.company.project.converter.CustomDateTimeConverter</converter-class>
</converter>

Related Articles

Advertisements

13 Responses to JSF default timezone and date pattern

  1. Paulo says:

    Após mais de um dia tendo problemas para inserir datas utilizando o (que estava armazenando as datas com um dia a menos do que a digitada) e tentar diversas outras soluções propostas na internet, utilizei a solução aqui proposta e resolvi meu problema. Muito obrigado!!!

  2. Paulo says:

    utilizando o … f:ConvertDateTime (…

  3. Anonymous says:

    thanks

  4. Anonymous says:

    nice

  5. Raphael says:

    In JSF 2 you can define this in web.xml to use the system timezone:

    javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE
    true

  6. YurZ says:

    Thanks Raphael, javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE was exactly what I was looking for.

  7. Jutus Herz says:

    You talk about the pattern. But, sometimes, the date is simply dd/MM/yyy (no time), and sometime have time. How we can handle it?

  8. Michel says:

    Using jsf 1.2 and icefaces 1.8.2_7, I had to register the custom converter using id “javax.faces.DateTime” in order to enable it with .

  9. Michel says:

    Using jsf 1.2 and icefaces 1.8.2_7, I had to register the custom converter using id “javax.faces.DateTime” in order to enable it with f:convertDateTime tags.

  10. right using javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE define timeZone, but who define the format unused tag f:convertDateTime, can you define this in web.xml a configuration?
    I’m from Ecuador, sorry for the bad English…

  11. Anonymous says:

    This is quite helpful, thanks a lot, worked perfectly!

  12. Pingback: JEE 6 and Dates | Natural Born Coder

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: