6

DB value of Date is:

04-OCT-10

Bean method returns:

Mon Oct 04 00:00:00 EEST 2010

JSF returns:

03.10.2010

JSF code:

...
     <h:outputText value="#{paym.dueDate}" >
            <f:convertDateTime pattern="dd.MM.yyyy"/>
     </h:outputText>
...

What reason(s),that JSF displays Date value incorrectly?

thank you

sergionni
  • 13,290
  • 42
  • 132
  • 189

2 Answers2

19

The JSF date converters defaults to UTC timezone. But your date is apparently stored using EEST timezone which is some hours beyond UTC (GMT+3 to be precise). When intepreting those dates using UTC timezone (as JSF by default does), you will get hours back in time and thus the previous day will be represented.

You need to explicitly specify the timezone in <f:convertDateTime>:

<f:convertDateTime pattern="dd.MM.yyyy" timeZone="GMT+3" />
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • @BalusC strange, didn't help. If I add z param to jsf converter,it displays 'CEST' zone. – sergionni Jun 05 '11 at 16:07
  • Maybe it's invalid for Java. I don't know all timezone IDs from top of head. Try `timeZone="CET"` or an explicit GMT+-n instead such as `timeZone="GMT+1"`. – BalusC Jun 05 '11 at 16:09
  • oops, it displays CEST after setting CET as you've advised, and without CET param it displays GMT – sergionni Jun 05 '11 at 16:09
  • I've tested 'GMT+3' works,less doesnt' work. Is it stable? – sergionni Jun 05 '11 at 16:14
  • According [this page](http://www.timeanddate.com/library/abbreviations/timezones/eu/eest.html) it's indeed GMT+3 (UTC+3). Whether that's stable is hard to say. Is the date also consistently saved and transferred in the same timezone? Is the daylight saving time taken into account? Etc. The best practice is to use UTC **all the way** and format it only during input/output. See also [Daylight saving time and timezone best practices](http://stackoverflow.com/questions/2532729/daylight-saving-time-and-timezone-best-practices). – BalusC Jun 05 '11 at 16:17
  • yes,you are correct, I'm resided in Ukraine.And if I left this 'timeZone' param in JSF,what value will see my collegs from Norway?I just thinking how to do it stable – sergionni Jun 05 '11 at 16:20
  • If it's the same webserver and database, then the colleagues from Norway will see the same value as you see. It's not dependent on the client side, but on the server side. – BalusC Jun 05 '11 at 16:23
  • @BalusC ,perhaps, offtopic, how to get DB timezone? is it param for to_char function? – sergionni Jun 05 '11 at 16:55
  • Technically, this depends on the DB server used, but it's usually configureable and it usually defaults to the operating system platform default timezone (in any case, this is true for the Java's JVM). It's however usually explained in the DB server's documentation. E.g. MySQL: http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html – BalusC Jun 05 '11 at 17:05
  • You're welcome. It's a pretty tough and nasty subject, time zones. Good luck. – BalusC Jun 05 '11 at 17:07
1

Also consider This answer Which might be best suited for countries with different summer/winter time, and when both your server timezone and jsf app user timezone is such a timezone.

Community
  • 1
  • 1
Olivier Tonglet
  • 3,312
  • 24
  • 40