Calendar with java.sql.Date

UI Components for JSF
Post Reply
rcasha
Posts: 8
Joined: 19 Feb 2011, 09:28

25 Feb 2011, 12:07

I am trying to use the calendar component bound to properties of type java.sql.Date and java.sql.Timestamp. I have created the necessary converters from String to these datatypes, and specified the converters to the calendar component, but with these bindings, the calendar never sets the value. It gets the existing value, and displays it but ignores the pattern.

Is it, or should it be, possible, to use the calendar component with java.sql.Date and other types that inherit from java.util.Date?

User avatar
ydarcin
Posts: 258
Joined: 04 Jan 2009, 19:02
Location: Turkey

25 Feb 2011, 16:23

Hi,

maybe you can use a converter where you take the selected value from calendar and convert it to any type you want.

Yigit

rcasha
Posts: 8
Joined: 19 Feb 2011, 09:28

25 Feb 2011, 19:22

I thought that converters only convert from String to the target object and vice versa. Can I have a converter that takes the calendar's java.util.Date and convert it to java.sql.Date before it is set in the property?

robert.m
Posts: 226
Joined: 07 Dec 2010, 22:52
Location: Salzburg/Austria

25 Feb 2011, 20:04

Is there a specific reason why you need to use java.sql.Date?

Anyways, as you already mentioned, java.sql.Date inherits from java.util.Date which makes it possible to cast a java.sql.Date to a java.util.Date:

Code: Select all

java.util.Date date1 = new Date();
java.sql.Date date2 = (java.util.Date)date1;
Accordingly, you could store your Date using java.sql.Date in your Bean and add another get-Method which returns a java.util.Date using a cast of the Bean's attribute and a setter method which converts the java.util.Date parameter to the java.sql.Date.

Code: Select all

class DateBean {

	java.sql.Date date;

	java.util.Date getCalendarDate() {
		return (java.util.Date)this.date;
	}

	void setCalendarDate(java.util.Date date) {
		this.date = new java.sql.Date(date.getTime());
	}

	java.sql.Date getDate() {
		return this.date;
	}

	void setDate(java.sql.Date date) {
		this.date = date;
	}
}
for your calendar you can use the dateBean.calendarDate-Property

rcasha
Posts: 8
Joined: 19 Feb 2011, 09:28

25 Feb 2011, 21:30

Yes the property is a jpa entity, they use java.sql types. I'll try this, thanks.

hnoor0033
Posts: 1
Joined: 04 Feb 2016, 10:05

04 Feb 2016, 10:22

Can I have a converter that takes the calendar's java.util.Date and convert it to java.sql.Date before it is set in the property?
=== free slots ===

kukeltje
Expert Member
Posts: 9605
Joined: 17 Jun 2010, 13:34
Location: Netherlands

11 Feb 2016, 13:49

Don't do it there. Do it in your persistence layer. Bad design to have java.sql.Date in your model...

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 24 guests