public class DateEditor extends PropertyEditorSupport {
public static final DateFormat DATEFORMAT = new SimpleDateFormat("yyyy-MM-dd");
public static final DateFormat TIMEFORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static final DateFormat ONLY_TIMEFORMAT = new SimpleDateFormat("HH:mm:ss");
private DateFormat dateFormat;
private boolean allowEmpty = true;
/**
* Parse the Date from the given text, using the specified DateFormat.
*/
@Override
public void setAsText(String text) throws IllegalArgumentException {
if (text == null || "".equals(text)) {
setValue(null);
} else {
try {
boolean time = text.contains(":");
boolean date = text.contains("-");
if(date && time){
setValue(TIMEFORMAT.parse(text));
}
if(date && !time){
setValue(DATEFORMAT.parse(text));
}
if(!date && time){
setValue(ONLY_TIMEFORMAT.parse(text));
}
} catch (ParseException ex) {
throw new IllegalArgumentException("Could not parse date: " + ex.getMessage(), ex);
}
}
}
/**
* Format the Date as String, using the specified DateFormat.
*/
@Override
public String getAsText() {
Date value = (Date) getValue();
DateFormat dateFormat = this.dateFormat;
if(dateFormat == null)
dateFormat = TIMEFORMAT;
return (value != null ? dateFormat.format(value) : "");
}
}
前台时间转后台public class DateEditor extends PropertyEditorSupport { public static final DateFormat DATEFORMAT = new SimpleDateFormat("yyyy-MM-dd"); public static final DateFormat TIMEFORMAT = ...