【问题标题】:Integrating Rich Calendar with Joda Time将 Rich Calendar 与 Joda Time 集成
【发布时间】:2012-01-24 11:25:05
【问题描述】:
我希望在我的 JSF 应用程序中使用 Rich:Calendar。我计划使用 CalendarDataModel 来限制某些日期的选择,例如周末、节假日等。我们使用的日期格式基于 Joda Time。
问题是 Rich:Calendar 是基于 JDK 日历的,所以我正在考虑使用 JDK 日历进行显示,并在以后持久化日期时将其转换为 jodatime。
有没有人使用 Rich:Calendar/Joda Time 来选择日历数据模型的日期,您能否分享您的宝贵想法。谢谢
【问题讨论】:
标签:
java
jsf
richfaces
jodatime
【解决方案1】:
您可以使用自定义 FacesConverter 在rich:calendar 中启用jodatime-objects,无需使用JDK Calendar。
只需在rich:calendar 中指定转换器,并实现Converter 接口的两个方法(参见使用LocalDate 的示例)。
JSF w/Richfaces 4.2
<rich:calendar value="#{myObject.myLocalDate}" datePattern="dd.MM.yyyy" converter="localDateConverter"/>
您的转换器类:
@FacesConverter(value = "localDateConverter")
public class LocalDateConverter implements Converter {
public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String localDateInTextFormat) {
return DateTimeFormat.forPattern("dd.MM.yyyy").parseLocalDate(localDateInTextFormat);
}
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object localDate) {
return DateTimeFormat.forPattern("dd.MM.yyyy").print((LocalDate)localDate);
}
}