FirefoxとStruts 2はDateでドライフレーム


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

import org.apache.struts2.util.StrutsTypeConverter;

public class DateConverter extends StrutsTypeConverter {
	/*
	 * http://blog.163.com/ztxz_179/blog/static/10520599520122575940933/
	 *  classpath  xwork-conversion.properties 
	 *  :java.util.Date=com.sohu.video.ad.struts.converter.DateConverter
	 * */
	
	private static final String FORMATDATE = "yyyy-MM-dd";

	private static final String FORMATTIME = "yyyy-MM-dd HH:mm:ss";

	@SuppressWarnings("unchecked")
	@Override
	public Object convertFromString(Map context, String[] values, Class toClass) {

		if (values == null || values.length == 0) {

			return null;

		}

		//  

		SimpleDateFormat sdf = new SimpleDateFormat(FORMATTIME);

		Date date = null;

		String dateString = values[0];

		if (dateString != null) {

			try {

				date = sdf.parse(dateString);

			} catch (ParseException e) {

				date = null;

			}

			if (date == null) {

				sdf = new SimpleDateFormat(FORMATDATE);

				try {

					date = sdf.parse(dateString);

				} catch (ParseException e) {

					date = null;

				}

			}

		}

		return date;

	}

	@SuppressWarnings("unchecked")
	@Override
	public String convertToString(Map context, Object o) {

		if (o instanceof Date) {

			SimpleDateFormat sdf = new SimpleDateFormat(FORMATTIME);

			return sdf.format((Date) o);

		}

		return "";

	}

}