Tuesday, October 23, 2007

Date is displayed wrong-decrease 1 day(JSF)

Have you ever get confused because your Date is displayed wrong (i.e. decreased 1 day) when you use h:outputText?
This might happen because JSF spec says that date converter defaults to using standard UTC timezone and you're happen to live in some other timezone, in my case it is GMT+07.

My first idea was to build a date converter which use the default timezone in the server and use f:converter to specify my converter id. Somehow the date pattern can't be passed to my converter, that's sad, I don't want to force a certain pattern.

My second idea is simply to create a method in my utility bean called
public String getTimeZone() which will return the default timezone ID, and use it to specify the timezone attribute in f:convertDateTime facet.

so the outcome looks like this :
<h:outputText value="#{track.movedDate}">
<f:convertDateTime pattern="MM/dd/yyyy" type="date" timeZone="#{constants.timeZone}"/>
</h:outputText>

All that needs to be done is to put the convertDateTime facet in every outputText.

11 comments:

SammyRulez said...

I replaced date format with my own implementation with DEFAULT_TIMEZONE = System.timezone which is right in 90%

Santoso said...

I'm taking the simple approach while allowing a different timezone to be used (if required).

How do you replace the date format?

Anonymous said...

Both timezone and date pattern can be customized by writing custom date time converter. Read below link for more information.

http://venkatsadasivam.wordpress.com/2008/07/08/jsf-default-timezone-and-date-pattern/

Anonymous said...

Hello All ,
I had faced an issue, that convertDateTime tag is setting a day less than what user entered. I set the timezone attribute into the default timezone and it resolved the issue.
JSF page
---------
f:convertDateTime pattern="MMddyyyy" timeZone="#{yourBean.timeZone}"

Bean
-----
TimeZone timeZone = TimeZone.getDefault();

Thanks Everyone...

Anonymous said...

Thank you, Santhosh!! This fixed my problem. I had no idea what I was going to use for a TimeZone String!

Anonymous said...

hi...

i (still) get confused with this decreasing date...

in many computers my program seems to work (no decreasing date) but in some computers it decreased 1 day :(

i try to change the regional setting too but it won't work...

please help me with this thing guyz...

* i'm sorry my English wasn't very good :p *

oriolmartiribas said...

Thanks Santhosh,
I solve my problem with your comments.

Anonymous said...

Santhosh !!!
You saved my day too!
Thank you so much!
Hugs from brasil,
gisah

Anonymous said...

Good post Santoso, you opened the way to solve our problem, and Santhosh... Great Tip!

Pedram said...

thank you Santhosh for your solution. The problem had kept me going around code for a while and now it's solved.

Peter said...

Setting the below context-param solves the problem also and much easier.


javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE
true



Source: http://stackoverflow.com/questions/2689245/fconvertdatetime-displays-wrong-date