You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by "fisher.yu" <us...@gmail.com> on 2008/01/17 09:38:52 UTC
Re: Dynamic Converter
My solution is use costomized Converter to read the timezone from parameter.
Hope it useful ^^
<h:outputText value="#{var.item.createDatetime}"
id="ticketCreateDate" >
<f:converter
converterId="com.yinhoo.ers.jsf.converter.dateConverter" />
<f:attribute name="dateStyle" value="yyyy-MM-dd HH:mm:ss"/>
<f:attribute name="timeZone"
value="#{userBean.timeZoneId}"/>
<%/*<f:convertDateTime pattern="yyyy-MM-dd HH:mm:ss"
timeZone="#{userBean.timeZone}"/>*/%>
</h:outputText>
public class DateConverter implements Converter {
private static final String CUSTOM_FORMAT_PARAMETER_NAME = "dateStyle";
private static final String TIME_ZONE_PARAMETER_NAME = "timeZone";
private static final String DEFAULT_STYLE = "yyyy-MM-dd";
private String format;
private TimeZone tz;
public Object getAsObject(FacesContext context, UIComponent component,
String arg2) throws ConverterException {
Date date = null;
setFormat(component);
SimpleDateFormat df = this.getFormater();
try {
date = df.parse(arg2);
} catch (ParseException e) {
throw new ConverterException("Not a valid date format");
}
return date;
}
private SimpleDateFormat getFormater() {
SimpleDateFormat df = new SimpleDateFormat(format);
if(tz!=null) {
df.setTimeZone(tz);
}
return df;
}
@SuppressWarnings("unchecked")
private void setFormat(UIComponent component) {
Object styleAttribute = component.getAttributes().get(
CUSTOM_FORMAT_PARAMETER_NAME);
Object timeZoneAttr =
component.getAttributes().get(TIME_ZONE_PARAMETER_NAME);
if(timeZoneAttr!=null) {
this.tz = TimeZone.getTimeZone((String)timeZoneAttr);
}
if (styleAttribute == null) {
this.format = DEFAULT_STYLE;
} else {
this.format = (String) styleAttribute;
}
}
public String getAsString(FacesContext arg0, UIComponent component,
Object arg2) throws ConverterException {
Date date = null;
setFormat(component);
try {
date = (Date) arg2;
} catch (Exception e) {
throw new ConverterException("Not a valid date format");
}
SimpleDateFormat df = this.getFormater();
String dateString = df.format(date);
return dateString;
}
}
:handshake::handshake::handshake:
--
View this message in context: http://www.nabble.com/Dynamic-Converter-tp454790p14914379.html
Sent from the MyFaces - Users mailing list archive at Nabble.com.