該值應該是java.util.Date類型。
因此,從的XMLGregorianCalendar獲得Date對象是這樣的:
record.toGregorianCalendar().getTime();
UPDATE:
你可以使用這樣的:
<h:outputText value="#{bean.value.record.toGregorianCalendar().time}" >
<f:convertDateTime pattern="dd.MM.yy" />
</h:outputText>
這實際上應該工作,但因爲你說你得到一個IllegalAccessException,我不確定是否有確切的原因。
另外,您也可以編寫自己的轉換器,如果你想,轉換器將是這樣的:
如果你想使用,你會用dateTimeConverter使用相同的屬性,那麼你需要將它們傳遞的屬性中組件和擴展DateTimeConverter這樣的:
@FacesConverter("com.examples.Date")
public class XMLGregorianCalConverter extends DateTimeConverter {
private static final TimeZone DEFAULT_TIME_ZONE = TimeZone.getTimeZone("GMT");
private String dateStyle = "default";
private Locale locale = null;
private String pattern = null;
private String timeStyle = "default";
private TimeZone timeZone = DEFAULT_TIME_ZONE;
private String type = "date";
@Override
public Object getAsObject(FacesContext context, UIComponent component, String newValue) {
// TODO Auto-generated method stub
return null;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
Map<String, Object> attributes = component.getAttributes();
if(attributes.containsKey("pattern")){
pattern = (String) attributes.get("pattern");
}
setPattern(pattern);
if(attributes.containsKey("locale")){
locale = (Locale) attributes.get("locale");
}
setLocale(locale);
if(attributes.containsKey("timeZone")){
timeZone = (TimeZone) attributes.get("timeZone");
}
setTimeZone(timeZone);
if(attributes.containsKey("dateStyle")){
dateStyle = (String) attributes.get("dateStyle");
}
setDateStyle(dateStyle);
if(attributes.containsKey("timeStyle")){
timeStyle = (String) attributes.get("timeStyle");
}
setTimeStyle(timeStyle);
if(attributes.containsKey("type")){
type = (String) attributes.get("type");
}
setType(type);
XMLGregorianCalendar xmlGregCal = (XMLGregorianCalendar) value;
Date date = xmlGregCal.toGregorianCalendar().getTime();
return super.getAsString(context, component, date);
}
}
,並在網頁上使用這樣的:
<h:outputText value="#{bean.value.record}" >
<f:converter converterId="com.examples.Date" />
<f:attribute name="pattern" value="dd.MM.yy" />
</h:outputText>
來自此問題的啓發/複製的代碼:JSF convertDateTime with timezone in datatable
是的,在Java中很容易,但是如何在JSF中執行此操作? – Thor 2012-07-29 18:09:27
Update1引發IllegalAccessException。正如預期的那樣,您的替代方法正在工作,但不靈活,因爲_pattern_在「XMLGregorianCalConverter」中進行了硬編碼。 – Thor 2012-07-29 19:16:46
@Thor,我已經更新了關於您的問題的答案。 – Ravi 2012-07-30 02:55:58