Citation:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.jsp.JspException;
import org.apache.struts.taglib.html.TextTag;
public class DateTag extends TextTag {
private String format = "dd-MM-yyyy";
private DateFormat formater = new SimpleDateFormat(format);
protected String formatValue(Object object) throws JspException {
if( object != null && object instanceof Date ) {
return formater.format((Date)object);
} else {
return super.formatValue(object);
}
}
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
formater = new SimpleDateFormat(format);
}
}
Et dans mon converter le format que je souhaite avoir pour insérer ma date dans ma BDD :
Citation:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class DateConverter implements Converter {
private static final Log logger = LogFactory.getLog(DateConverter.class);
private Object defaultValue = null;
public DateConverter() {
this.defaultValue = null;
}
public DateConverter(Object defaultValue) {
this.defaultValue = defaultValue;
}
public Object convert(Class type, Object value) {
Date laDate = new Date();
if (value == null) {
return (defaultValue);
}
if (value instanceof Date) {
return (value);
}
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
laDate = sdf.parse(value.toString());
return laDate;
} catch (ParseException e) {
logger.debug("Unable to convert " + value + " into Date format.");
return defaultValue;
}
}
}
Pourrais je avoir des informations supplémentaires ?