Using java.util.Calendar with the calendar form element

UI Components for JSF
Post Reply
jsp
Posts: 4
Joined: 14 Feb 2011, 16:32

14 Feb 2011, 16:41

Alot of our date related fields use java.util.Calendar instead of java.util.Date.

<p:calendar id="blah" value="#{myBean.myObject.myDate.time}" />

I get errors at runtime if field myDate (which is a java.util.Calendar) is null.

Is there a way around this?

User avatar
bumble.bee
Posts: 723
Joined: 29 Sep 2010, 21:39
Location: United States

15 Feb 2011, 16:05

Just set a default value in your bean. [something like Calendar.getInstance();]

jsp
Posts: 4
Joined: 14 Feb 2011, 16:32

15 Feb 2011, 17:35

The date/calendar fields are stored in the database. Null is a valid value for many of the date/calendar fields so providing a default value isn't really an option.

jsp
Posts: 4
Joined: 14 Feb 2011, 16:32

15 Feb 2011, 20:54

I had to add a custom converter allow the primefaces Calendar component to convert to and from a java.util.Calendar.

While not ideal, I used the code that primefaces uses to convert to and from java.util.Date after getting or setting the date from the java.util.Calendar object.

It would be nicer if primefaces either supported this natively or if there at least was a primefaces DateTimeConverter that I could extend.

jsp
Posts: 4
Joined: 14 Feb 2011, 16:32

15 Feb 2011, 20:56

Oh yeah, I also changed my page:

From this:
<p:calendar id="blah" value="#{myBean.myObject.myDate.time}" />

To this:
<p:calendar id="blah" value="#{myBean.myObject.myDate}" converter="primefacesCalendarConverter"/>

adneycandy
Posts: 1
Joined: 01 Apr 2016, 08:10

01 Apr 2016, 08:12

very Nice

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 29 guests