ITeye博客

struts日期转换器

lichaozhangobj · 2011-01-15 21:51 · struts · Web前端 · Java · Apache · Struts · Markdown

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.beanutils.Converter;

public class DateConverter implements Converter {

	@SuppressWarnings("unchecked")
	public Object convert(Class clazz, Object value) {
		if(value==null || "".equals((String)value)) return null;
		if(value instanceof Date) return value;
		DateFormat convert = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return convert.parse((String) value);
		} catch (ParseException e) {
			DateFormat convert2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				return convert2.parse((String) value);
			} catch (ParseException e1) {}
		}
		return null;
	}

}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。