How to map IceFaces component on a java.util.Calendar field?

后端 未结 2 1821
無奈伤痛
無奈伤痛 2021-01-23 05:28

Does anybody knows how can component be mapped on a java.util.Calendar field, not java.util.Date?

I am

相关标签:
2条回答
  • 2021-01-23 05:34

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

    0 讨论(0)
  • 2021-01-23 05:38

    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.

    0 讨论(0)
提交回复
热议问题