JSF default timezone and date pattern
July 8, 2008 13 Comments
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>
Recently I found this link in myfaces.
http://wiki.apache.org/myfaces/FAQ#Date
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!!!
utilizando o … f:ConvertDateTime (…
thanks
nice
In JSF 2 you can define this in web.xml to use the system timezone:
javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE
true
Thanks Raphael, javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE was exactly what I was looking for.
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?
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 .
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.
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…
This is quite helpful, thanks a lot, worked perfectly!
Pingback: JEE 6 and Dates | Natural Born Coder