Board index JavaServer Faces General Calendar selection goes 1 day in the past

Calendar selection goes 1 day in the past

Components, Ajax Framework, Utilities and More.

jbx

Posts: 5
I am encountering a wierd problem where the date I submit through the Calendar control goes back one day when I display it in the subsequent page. So I select 12th March 2010, and the date passed to the backing bean becomes 11th March 2010.

All I have is the following:

@Stateless
@LocalBean
@Named
public class DateBean implements Serializable
{
    private Date date;

    public Date getDate()
    {
        return date;
    }

    public void setDate(Date date)
    {
        this.date = date;
    }
}


and

@Named
@SessionScoped
public class DateController implements Serializable
{
    @EJB
    private DateBean datebean;

    public String processDate()
    {       
        return "selectedDate";
    }
}


In the form page I have:

     <h:form>
            <p:calendar value="#{dateBean.date}"  pattern="dd/MM/yyyy" />

        <h:commandButton value="Set Date" action="#{dateController.processDate}" />
        </h:form>


and in the result page named selectedDate.xhtml I have:

The date you selected was:
             <h:outputText value="#{dateBean.date}">
                <f:convertDateTime  pattern="dd/MM/yyyy" />
            </h:outputText>


I have also deployed the PrimeFaces showcase war file on my Glassfish v3 and the same problem also occurs. I select the date and submit, and the resultant date text becomes one day less than what I selected.

What could it be?


Posts: 15040
Location: Cybertron

Are you sure the date passed is 1 day behind or the date display by outputText is behind. Can you log date on server side to see it.

    public void setDate(Date date)
    {
        this.date = date;
        System.out.println(date);
    }
PrimeFaces Lead

jbx

Posts: 5
Hi,

I added that log.

I chose 12th March 2010.

The log said:
INFO: Fri Mar 12 00:00:00 CET 2010

The output text then said:
The date you selected was: 11/03/2010

As I said in my previous post, this problem is also happening with the PrimeFaces Showcase war deployed on the same Glassfish v3 server.

Could it be something related to timezones?

Thanks.


Posts: 15040
Location: Cybertron

So the date passed is correct but h:outputText is displaying it as one day behind, defining a timezone on your f:convertDateTime should fix it. It is not a PrimeFaces problem.

See question 'I used the JSF DateTimeConverter to format an input field into a Date, but the dates were sometimes one day behind. Why?' at

http://ocpsoft.com/java/undocumented-jsf/
PrimeFaces Lead

jbx

Posts: 5
Yes that was the problem. Actually I found a more global way to set this to the system timezone, maybe its useful for other people looking for a solution to this problem.

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


I wonder why this property isn't set like this by default and overridden by the developer if required... but I guess otherwise life would be too easy. :(


Posts: 15040
Location: Cybertron

I think it is because of backward compatibility as this is a JSF 2.0 only feature.
PrimeFaces Lead


Return to General