Problem with the time when editing a java web application record using ejb + jsf?

0

I'm using Glassfish server 4.1.1, java EE7 web, jdk 1.8 and jsf 2.2

When editing or keeping a record the time or good date also differ between what is stored in the bdd (which is correct) and what the datatable shows (which is incorrect). The following image shows the data table:

and now what is shown in the bdd field (correct date)

Methods get and set of the class and reference to the field of the table in the user class. java

public Date getUpdated () {         return updated;     }

public void setUpdated(Date updated) {
    this.updated = updated;
}  

public Date getCreated () {         return created;     }

public void setCreated(Date created) {
    this.created = created;
}

@Column (name="CREATED")     @Temporal (TemporalType.TIMESTAMP)     private Date created;

@Column (name="UPDATED")     @Temporal (TemporalType.TIMESTAMP)     private Date updated;

form.xml

<h:outputLabel value="Modificado:" for="updated"/>
        <h:inputText id="updated" value="#{usuarioController.selected.updated}" title="Actualizado" disabled="true">
            <f:convertDateTime pattern="dd/MM/yyyy HH:mm:ss"/>
        </h:inputText>

<h:outputLabel value="Creado    :" for="created"/>
        <h:inputText id="created" value="#{usuarioController.selected.created}" title="Creado" disabled="true">
          <br></br>
        <f:convertDateTime pattern="dd/MM/yyyy HH:mm:ss"/>
        </h:inputText>

Methods in the controller to create and edit a user

public String agregar(){
        
        Date d= new Date();
        selected.setCreated(d);
        selected.setUpdated(d);
        dao.create(selected);        
        return "/usuario/index";
        
    }

public String edit(int codigo){
    selected = dao.find(codigo);
    return "/usuario/edit";    
}

public String guardar(){
    Date d = new Date();        
    selected.setUpdated(d);
    dao.edit(selected);
    return "index";
}

PD. I am using the own database of netbeans that is included in the services

    
asked by Cargeins 26.01.2017 в 22:36
source

1 answer

0

The problem is clearly the time zone. Objects of type Date have no knowledge of the time zone, therefore, JSF as your JPA persistence adapter are performing conversions incorrectly. To successfully convert the dates, all the components involved in the system must communicate using the same time zone in the configuration.

The first component is the operating system, which maintains the local time using a local time zone .

The second component is the JVM , which if you have not defined the system property user.timezone , will use the local time zone as the return value for TimeZone.getDefault() initially, which represents the zone by default in several of the following components.

The third component is the application server and the web applications deployed.

Web applications (and their packaged dependencies) generally have free access to invoke TimeZone.setDefault() , an invocation to this method might seem harmless, however it is completely harmful ; Imagine that the Web Application A works with the time zone Etc/UTC because it is the local time zone and performs all the conversions according to this assumption, later a Web Application B changes the time zone invoking TimeZone.setDefault() , the result is more than obvious.

The object Date does not internally save information about the time zone, it represents a single time instant not affected by time zone or summer time, that is, to show the correct date it is necessary to convert this universal value to the zone desired time All this is usually done by the following components.

When using JPA , one of the components of the Web Application is the persistence adapter , which decides how to convert an object of type Date to the Due representation for an SQL statement (whether or not it is a ready statement), this conversion usually uses TimeZone.getDefault() to perform this conversion. If the deployed applications do not have the bad practice of invoking TimeZone.setDefault() , this component will not present any conversion problem.

The framework of presentation or view is an indispensable component of the Web Application, this, like the persistence adapter, performs conversions. In the case of JSF , date conversions are defined through the <f:convertDateTime/> tag, such as in the problem example. One of the attributes of this tag is timeZone to define the desired time zone for the conversion. According to the documentation, this attribute does not have a default value, therefore, the date shown for the same time will be different, depending on the JSF implementation used.

The fastest solution to this problem in specifics is apparently to define the attribute timeZone in <f:convertDateTime/> , something similar to the following code:

<h:outputLabel value="Creado    :" for="created"/>
<h:inputText id="created" value="#{usuarioController.selected.created}" title="Creado" disabled="true">
    <br></br>
    <f:convertDateTime pattern="dd/MM/yyyy HH:mm:ss" timeZone="America/Mexico_City"/>
</h:inputText>

An alternative solution would be to use a persistence adapter with support for the class LocalDateTime , which represents a local date and time (without a time zone). Depending on the implementation of JSF and application server, it may be necessary to write and register a converter.

Links with more information:

answered by 05.02.2017 в 00:41