3 Replies Latest reply on Nov 19, 2007 5:02 AM by Olivier Thierry

    Problem retrieving dates from database

    Olivier Thierry Apprentice

      Hi,

      I have a EJB3 entity with a java.util.Date attribute defined as following :

      private java.util.Date dateDebut;
      
      @javax.persistence.Column(name = "DATE_DEBUT", nullable = false, insertable = true, updatable = true)
      @javax.persistence.Temporal(javax.persistence.TemporalType.DATE)
      public java.util.Date getDateDebut()
      {
       return dateDebut;
      }
      
      public void setDateDebut(java.util.Date value)
      {
       this.dateDebut = value;
      }


      But when I run SQL queries, the dateDebut attribute contains java.sql.Date data instead of java.util.Date ! So then I encounter problems when formatting this date ... What should I do so that SQL dates are converted to java.util.Date in my EJB3 entities ?

      Thanks in advance,

      P.S. : I run on JBoss 4.2.0.GA with MySQL database (same problem with HSQLDB anyway).

        • 1. Re: Problem retrieving dates from database
          Elias Ross Master

          You can do something like this:

          
          public void setDateDebut(java.util.Date value)
          {
           this.dateDebut = new java.util.Date(value.getTime());
          }
          


          • 2. Re: Problem retrieving dates from database
            Olivier Thierry Apprentice

            Thanks, it works.

            Anyway I thought this was what caused my formatting problems, but I still have them ...

            For example, I have a java.util.Date variable which contains "Thu Nov 15 00:00:00 CET 2007" value.

            If I format it with SimpleDateFormat :

            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
            System.out.println(sdf.format(membreUniteOrganisation.getDateDebut()));


            It prints "15/11/2007" which is right.

            But if I format it with <f:convertDateTime> with datePattern as "dd/MM/yyyy" :

            <h:outputText value="#{membre.dateDebut}">
             <f:convertDateTime pattern="#{messages['datePattern']}" />
            </h:outputText>


            It prints "14 nov. 2007" which is wrong of course !

            Any idea why ?

            • 3. Re: Problem retrieving dates from database
              Olivier Thierry Apprentice

              I found my problem. You just have to know that <f:convertDateTime> works by default with UTC time and not with the date variable timezone, which is not the case with java.util.SimpleDateFormat. So you have to explicitely set the timezone attribute for <f:convertDateTime>

              Something like that :

              <h:outputText value="#{membre.dateDebut}">
               <f:convertDateTime pattern="#{messages['datePattern']}" timeZone="GMT+1" />
              </h:outputText>