views:

804

answers:

2

Does anybody knows how can component <ice:selectInputDate> be mapped on a java.util.Calendar field, not java.util.Date?

I am using from IceFaces version 1.8.2, the component <ice:selectInputDate>. This component requires to be bound with a java.util.Date proeprty. For example, value="#{bean.myDate}", the myDate field must be of type java.util.Date. But I need my date field to be of type java.util.Calendar.

My trials: I have tried to use standard converter or a custom one:

  1. Standard one: <f:convertDateTime pattern="dd/MM/yyyy" /> it formats correct the value in GUI, but when setting it on the property bean.myDate of type Calendar I get following error message:

    [5/3/10 12:09:18:398 EEST] 00000021 lifecycle I WARNING: FacesMessage(s) have been enqueued, but may not have been displayed. sourceId=j_id12:j_id189:myDate[severity=(ERROR 2), summary=(/WEB-INF/xhtml............file.xhtml @507,51 value="#{bean.myDate}": Can't set property 'myDate' on class 'bean' to value '5/11/10 3:00 AM'.), detail=(/WEB-INF/xhtml........file.xhtml @507,51 value="#{bean.myDate}": Can't set property 'myDate' on class '...bean...' to value '5/11/10 3:00 AM'.)]

  2. Custom one: <f:converter converterId="c2d"/>

    • getAsObject - returns the java.util.Calendar object out of the submitted String.
    • getAsString - receives an Object, and returns the String formatted.

NOTE: this method was hacked so instead of expecting java.util.Calendar, to be complementary with getAsObject method. Instead, the hacked method getAsString, expects an java.util.Date, provided as parameter (by ice:selectInputDate) and returns the String formatted.

But still an error message occurs:

[5/3/10 12:55:34:299 EEST] 0000001f D2DFaceletVie E com.icesoft.faces.facelets.D2DFaceletViewHandler renderResponse Problem in renderResponse: java.util.GregorianCalendar incompatible with java.util.Date java.lang.ClassCastException: java.util.GregorianCalendar incompatible with java.util.Date at com.icesoft.faces.component.selectinputdate.SelectInputDate.getTextToRender(SelectInputDate.java:252)

Any hint is very useful! Thanks, Mihaela

+2  A: 

Wrap the Calendar property with another getter/setter returning/taking a Date.

private Calendar calendar;

public Date getCalendarDate() {
    return (calendar != null) ? calendar.getTime() : null;
}

public void setCalendarDate(Date date) {
    if (calendar == null) {
        calendar = Calendar.getInstance();
        calendar.clear(); // Avoid timezone issues.
    }
    calendar.setTime(date);
}

A JSF converter isn't going to work because this only does Object<-->String conversions, while we need a Object<-->Date conversion here. I don't do IceFaces, but there might be the chance that the particular component accepts a date string in a certain format pattern as well. You would need to find that out and then write the covnerter accordingly to convert Calendar<-->String according this string format pattern. The java.text.SimpleDateFormat is helpful in this.

BalusC
+2  A: 

Either do as BalusC suggests, or simply set value="#{yourBean.yourCalendar.time}.

Bozho
Drat, I knew it! This is definitely better. This by the way expects that the `yourCalendar` is not null, else you may face a `PropertyNotFoundException: null`.
BalusC
Thank you guys, your solution helps me a lot. Best of luck!
@blummihaela if an answer works for you, mark it as accepted (tick below the vote counter)
Bozho