2 Replies Latest reply on Jun 27, 2011 3:36 AM by Shervin Asgari

    Converting date correctly from YY to YYYY

    Shervin Asgari Master

      When the user types in 01.01.11, it interprets it to 01.01.0011 which is obviously incorrect. The correct value should have been 01.01.2011


      Anyone encountered this? How did you solve it? The xhtml is the following:



      <h:inputText value="#{budgetHandler.grantedFrom}">
        <s:convertDateTime type="date" pattern="dd.MM.yyyy"/>
      </h:inputText>



        • 1. Re: Converting date correctly from YY to YYYY
          Tim Evers Master

          From a pure mathematical/java/date perspective 01.01.11 is CORRECTLY translated to 01.01.0011. That is the absolute correct result. What if the application is being written for a history tool. How would they enter the year 11 AD if 11 was always translated to 2011. How do you know exactly what century 11 is in? Should that date be 2011, 1911, 1811? Every century is valid. Thus the only way to correctly convert 01.01.11 to a date is to represent it exactly - the year 11 AD.


          Now, how do you fix it. Quite simple really. Write your own converter. In your converter you will need to decide at what point does a 2 digit year convert from a 21st century year to a 20th century year. Does 01.01.99 mean 2099 or does it mean 1999. You'll need to write this yourself.


          Java is doing the right thing. It has no way of knowing what century a 2 digit year belongs in so... you'll have to tell it.

          • 2. Re: Converting date correctly from YY to YYYY
            Shervin Asgari Master

            Hi Tim.


            Thanks for the answer. I guess it makes sense when you put it like that.