0
votes

i have a problem with a PrimeFaces component. I have the same code that is in the page http://www.primefaces.org/showcase/ui/input/calendar.xhtml I'm using the component that only shows the time. Here is mi code.

Time.xhtml

<h:form id="form">

    <p:growl id="msgs" showDetail="true" />

    <h:panelGrid columns="2" cellpadding="5">

        <p:outputLabel for="time" value="Time:" />
        <p:calendar id="time" value="#{calendarView.date11}" pattern="HH:mm a" timeOnly="true"  />
    </h:panelGrid>

    <p:commandButton value="Submit" update="msgs" actionListener="#{calendarView.click}" icon="ui-icon-check" />

    <p:dialog modal="true" resizable="false" header="Values" widgetVar="dlg" showEffect="fold">
        <p:panelGrid id="display" columns="2" columnClasses="label,value">

            <h:outputText value="Time:" />
            <h:outputText value="#{calendarView.date11}">
                <f:convertDateTime pattern="HH:mm a" />
            </h:outputText>
        </p:panelGrid>
    </p:dialog>
</h:form>

And here is my bean.

CalendarView.java

private Date date11;

public void click() {
    RequestContext requestContext = RequestContext.getCurrentInstance();

    requestContext.update("form:display");
    requestContext.execute("PF('dlg').show()");
}

public Date getDate11() {
    return date11;
}

public void setDate11(Date date11) {
    this.date11 = date11;
}

The problem is when I run the Application, the calendar is addig six hours more.

enter image description here It could be the Time Zone? How can I solved this?

1
First off, I do not use the fail-fast java.util.Date API but in this case, I cannot reproduce this problem on a blank playground project having only a single XHTML file. I also tried in a real application. Hours and minutes are displayed on a <p:dialog> exactly as they are entered through a <p:calendar> without setting the timeZone attribute associated with an <f:convertDateTime>.Tiny

1 Answers

0
votes

When working with time zones, a good strategy would be :

  • Store Dates with default timeZone. Personally I prefer using UTC timezone for my tomcat, mysql etc, although this is not really necessary
  • Handle the timezone localization in your views. In primefaces most Date-related components like calendar orschedule will use the timeZone attribute which will handle the date conversion to local time, day time saving (DST) etc.

eg:

<p:calendar id="time" value="#{calendarView.date11}" pattern="HH:mm"
    timeOnly="true" 
    timeZone="Europe/Warsaw" />

Make sure to provide a timezone String key instead of a UTC/GMT offset. You can find your timezone key here

And one last thing: When using the timeOnly attribute, your p:calendar will still store a java.util.Date and the Year, Month, and Day will default to today. Make sure this won't be a problem with the DST. Always test your app for DST changes.