Gauthier Peel
Gauthier Peel

Reputation: 1518

JSF2 format date differently from same postgres timestamp column

In postgres 9.2 i have a column with a timestamp

CREATE TABLE pilot
(
  id bigint NOT NULL,
  birthdate timestamp without time zone NOT NULL,
  firstname character varying(50) NOT NULL,
  CONSTRAINT pilot_pkey PRIMARY KEY (id )
)

I inserted some rows directly with a java main with Hibernate :

    for (int i = 0; i < 10; i++) {
        Pilot p = new Pilot();
        p.setBirthdate(new Date());
        p.setFirstname("Johnson_" +tabchar[i] );
        em.persist(p);
    }

My property looks like this :

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "birthdate", nullable = false, unique = false)
public java.util.Date getBirthdate() {
    return birthdate;
}

And some rows are inserted by JSF2 relaying to hibernate : I 'm using JSF 2.1.13

    <h:form id="formPilot">
        <h:panelGrid columns="2">
            <h:outputLabel value="#{appMsg['pilot.firstname']}" />
            <h:inputText id="firstname" label="#{appMsg['pilot.firstname']}" value="#{pilotHolder.pilot.firstname}"
                required="true" />

            <h:outputLabel value="#{appMsg['pilot.birthdate']}" />
            <h:inputText id="birthdate" label="#{appMsg['pilot.birthdate']}" value="#{pilotHolder.pilot.birthdate}"
                required="true">
                <f:convertDateTime pattern="dd/MM/yyyy HH:mm:ss" timeZone="GMT+1"/>
            </h:inputText>

        </h:panelGrid>
        <h:commandButton id="merge" action="#{pilotAction.doMerge}"
            value="#{pilotHolder.pilot.id ne null ? appMsg['pilot.update'] : appMsg['pilot.create']}" />
    </h:form>   

In the DB all is OK, all the dates are correct (with the hours ok)

BUT When I show the list of Pilots though and show them on a page the date which were inserted by Hibernate without JSF have an error of 1 hour less, not the timestamps that i inserted with JSF ?

        <h:column>
            <f:facet name="header">
                <h:outputText value="#{appMsg['pilot.birthdate']}" />
            </f:facet>
            <h:outputText value="#{p.birthdate}" >
               <f:convertDateTime pattern="dd/MM/yyyy HH:mm:ss" timeZone="GMT+1"/>
            </h:outputText>
        </h:column>

What is strange is that i can't any difference in the DB using DBvisualizer, they all look similar, If I print the date after Hibernate has fetch them, all is ok.

If i look closer at the dates inserted by Hibernate and fetched back, they contain a field cdate not null of type Gregorian$Date. The dates inserted by JSF and fetched back have a cdate=null.

Why? and why are they interpreted differently by JSF2 ?

Upvotes: 1

Views: 1736

Answers (1)

Kerem Baydoğan
Kerem Baydoğan

Reputation: 10722

I think it is DAYLIGHT SAVING TIME related

Try this in your web.xml

<context-param>
    <param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name>
    <param-value>true</param-value>
</context-param>

Or you can tell the f:dateTimeConverter which timeZone to use. Just use a Time Zone that doesn't use daylight savings.

Upvotes: 3

Related Questions