---
title: "struts日期转换器"
author: "lichaozhangobj"
date: "2011-01-15T21:51:59Z"
tags: ["Apache", "Java", "Struts"]
categories: ["Web前端"]
slug: "struts"
draft: false
lastmod: "2011-01-15T21:51:59Z"
id: 875456
url: "https://lichaozhangobj.iteye.com/blog/875456"
---

```
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;
	}

}
```