morecore
morecore

Reputation: 1750

LocalDate (only) with primefaces calendar

I want to display a primefaces calendar element with an associated LocalDate in the Bean.

I used How to use java.time.ZonedDateTime / LocalDateTime in p:calendar as an example. The example as is works fine, BUT I don't need the time part and more importantly I don't want the time part of the calendar to be displayed.

The problem is that when I change the pattern and remove the time part:

<p:calendar id="localDate"
        pattern="dd-MMM-yyyy"
        value="#{bean.periodFrom}">
    <f:converter converterId="localDateConverter" />
    <p:ajax update="display" />
</p:calendar>

the converter is no longer called/ used at all. Which surprises me a bit.

Removing the pattern does not work neither.

Is there an alternative way to use the example without the time part? And why is the converter no longer called when I "simplify" the pattern?

JSF 2.2/ Java8/ Primefaces 6

Upvotes: 3

Views: 4061

Answers (2)

Murphy85
Murphy85

Reputation: 643

The ajax update is not being triggered, because you didn't specify any event on which the update should be triggered.

If you do not specify any event, the default event type "change" will be used.

  • p:calendar triggers a "change" event, when you enter the date in the inputText and blur
  • p:calendar triggers a "dateSelect" event, when you select a date in the calendar popup

In your case, you should insert multiple event listeners:

<p:ajax update="display" />
<p:ajax event="dateSelect" update="display" />

So your code will result in:

<p:calendar id="localDate"
        pattern="dd-MMM-yyyy"
        value="#{bean.periodFrom}">
    <f:converter converterId="localDateConverter" />
    <p:ajax update="display" />
    <p:ajax event="dateSelect" update="display" />
</p:calendar>

(By the way: It is not possible to combine multiple events in the same p:ajax component as you can read here https://stackoverflow.com/a/38319944/1643015)

Upvotes: 2

Alex Fire
Alex Fire

Reputation: 707

If you used the ZonedDateTimeConverter it couldn't work at all because LocalDate is not an instance of ZonedDateTime. That's why the converter will throw a ConverterException (because the instanceof check fails). You would see that if you insert a p:message for your p:calendar. The used pattern "dd-MMM-yyyy" does also not work for the ZonedDateTimeConverter because this pattern neither provides a time nor a timezone. That's why the conversion throws a DateTimeParseException and the converter throws a ConverterException, too. So if you want to work with LocalDate you have to implement your own converter than can deal with that class, which is very easy. Just take the ZonedDateTimeConverter and replace ZonedDateTime with LocalDate.

Upvotes: 0

Related Questions