1 2 Previous Next 19 Replies Latest reply on Feb 13, 2007 2:46 PM by gavin.king

    Problem with decrementing dates

    bfo81

      I have an entity Person with a Birthday (property annotated with @Temporal(TemporalType.DATE)).

      e.g. John Sixpack, 1976-07-04

      There's a JSF file for editing it, and it includes:

      <h:inputText value="#{person.birthday}">
       <f:convertDateTime type="date" />
      </h:inputText>


      Problem: The input text field shows the wrong date: 1976-07-03 instead of 1976-07-04.

      For debugging I added the following after the h:inputText
      <h:outputText value="#{person.birthday}" />
      <h:outputText value="#{person.birthday}">
       <f:convertDateTime type="both" />
      </h:outputText>

      and the page now displays:
      [__1976-07-03___] (input field with converter is wrong)
      1976-07-04 (normal output is correct)
      1976-07-03 23:00:00 (output field with converter is wrong)

      The database clearly says 1976-07-04 (it's a date field without time). But as you can see, the converter subtracts one hour, what results in a birthday one day earlier each time the user saves the edited entry.

      Well, it looks like a timezone (GMT+1) or summertime/daylight saving time problem, but I don't know what to do or where to start. Did anyone have similar experiences? Or a hint for solving this problem?

        • 1. Re: Problem with decrementing dates
          bfo81

          PS: I changed the timezone of my computer from GMT+1 to GMT-1, and now the date is regarded as 1976-07-04 01:00. So JSF really wants to perform a timezone adjustment.

          I know that there's a timeZone attribute for <f:convertDate />. But I think it's daft to artificially add this every time I use a date. Especially since client and server are running on the same machine here and sharing the same timezone.

          btw: Even the booking demo fails on my PC. If I enter a new booking from July 31st to August 1st everything is shown correctly on the confirm page, but the database entries are one day earlier.

          • 2. Re: Problem with decrementing dates
            bfo81

            No one had to deal with that problem before?

            • 3. Re: Problem with decrementing dates

              Well, the problem is not JSF specific. Java (like most programming languages) uses timestamps internally. This is useful for - well, timestamps. But it is not useful for birthdates etc.

              For java two persons born on the 1.1.1980 0:01:00.00 GMT+2 and 1.1.1980 0:01:00.00 GMT-2 are not born on the same day eventough the sane part of humanity would say that they are both born on the 1.1.1980.

              You can only avoid this by using an artificial timezone (that is utc) when dealing with dates.

              The only thing that suprises me is that the datetime converter use the local timezone instead of utc if no timezone is specified. I think this is a bug. Which JSF implementation do you use?

              Regards

              fhh

              • 4. Re: Problem with decrementing dates
                bfo81

                It's MyFaces 1.1.1 that came with JBoss JEMS Installer. But I tried it out with MyFaces 1.1.3 now and the problem persists (btw: with 1.1.3 sometimes I get a "IllegalStateException: No active application scope" in the server log, but the app still keeps working).

                Interresting: Some entities always have 23:00 and some always have 22:00. Always, even after restarting the application server. It's extremely weird and doesn't seem to make any sense.

                • 5. Re: Problem with decrementing dates
                  bfo81

                  Erm... I'm not sure if this is really a Java problem. Remember:

                  <outputText value="#{person.birthday}" />

                  shows the correct date. But the tags using a f:convertDateTime do it wrong. Maybe I'll try to implement my own DateConverter, then we'll see ;).

                  • 6. Re: Problem with decrementing dates
                    bfo81

                    Erm... I'm not sure if this is really a Java problem. Remember:

                    <outputText value="#{person.birthday}" />

                    shows the correct date. But the tags using a f:convertDateTime do it wrong. Maybe I'll try to implement my own DateConverter, then we'll see ;).

                    • 7. Re: Problem with decrementing dates
                      bfo81

                      What can I say? It is a Java problem AND a JSF problem, so to say ;)

                      I added -Duser.timezone=Europe/Berlin to JAVA_OPTS environment property. Now,

                      System.getProperty("user.timezone") is Europe/Berlin
                      TimeZone.getDefault().getID() is Europe/Berlin
                      TimeZone.getDefault().getDisplayName() is Zentraleuropäische Zeit (Central European Time)
                      and the offset is 3600000 ms (i.e. one hour)
                      and useDaylight is true

                      BUT in the DateTimeConverter class

                      getTimeZone().getId() is GMT
                      getTimeZone().getId() is Greenwich Zeit (Greenwich Time)
                      the offset is 0,
                      and useDaylight is false.

                      Well, the JVM has the right timezone set, but JSF ignores it and falls back to Greenwich Mean Time. I think I got a big fat bone to pick with the Apache MyFaces staff ;).

                      • 8. Re: Problem with decrementing dates
                        bfo81

                        Ohhhh... the JSF specs are the ones to lay the blame on. See http://wiki.apache.org/myfaces/FAQ#Date (section "Why are my dates displaying the wrong date/time").

                        Sorry, but overriding the standard server timezone is idiotic in my eyes. So you really must use a custom timezone in every convertDateTime tag or use the MyFaces Sandbox converter instead.

                        Maybe I had almost a pure monolog in this thread, but possibly it will be helpful for someone (and I can hardly believe I am the only one who will ever have this problem...)

                        • 9. Re: Problem with decrementing dates
                          iga3k

                          right now i'm cunfrunted with this issue.
                          did you get an elegant implementation?

                          • 10. Re: Problem with decrementing dates
                            muckwarrior

                             

                            "bfo81" wrote:


                            Maybe I had almost a pure monolog in this thread, but possibly it will be helpful for someone (and I can hardly believe I am the only one who will ever have this problem...)

                            I've also encountered the same problem. Thanks for pointing me in the right direction!

                            • 11. Re: Problem with decrementing dates

                              This is my solution:
                              Component:

                              
                              @Name("defaultTimeZone")
                              @Scope(ScopeType.APPLICATION)
                              @Intercept(NEVER)
                              public class DefaultTimeZone {
                               /**
                               * @return default TimeZone
                               */
                               @Unwrap
                               public TimeZone getDefaultTimeZone() {
                               return TimeZone.getDefault();
                               }
                              }
                              



                              Page:

                              <h:outputText value="#{myObj.date}">
                              <f:convertDateTime pattern="yyyy-MM-dd HH:mm:ss" timeZone="#{defaultTimeZone}" />
                              </h:outputText>


                              It is easy to change for example to userTimeZone where timeZone is extracted from database or something like this


                              • 12. Re: Problem with decrementing dates
                                gavin.king

                                Um .... do you mind if I just steal that code and put it in org.jboss.seam.core? Actually, if I did that, I should probably do a whole Timezone/TimezoneSelector thingy like what we have for locales...

                                I agree that the standard JSF behavior here is nuts. Or an alternative solution would be to introduce an <s:convertDateTime/> which is smart enough to default to the default server timezone ...

                                • 13. Re: Problem with decrementing dates

                                   

                                  "gavin.king@jboss.com" wrote:
                                  Um .... do you mind if I just steal that code and put it in org.jboss.seam.core?

                                  Not at all.
                                  "gavin.king@jboss.com" wrote:

                                  Actually, if I did that, I should probably do a whole Timezone/TimezoneSelector thingy like what we have for locales...
                                  I agree that the standard JSF behavior here is nuts. Or an alternative solution would be to introduce an <s:convertDateTime/> which is smart enough to default to the default server timezone ...


                                  I vote for it ! With all that cookies stuff like in localeSelector and mybe rename defaultTimeZone to serverTimeZone and add posibility to set default timezone in timezoneselector to serverTimezone

                                  • 14. Re: Problem with decrementing dates
                                    gavin.king
                                    1 2 Previous Next