カスタムタイムフォーマット変換

2375 ワード

カスタムタイムフォーマット変換
public class DateConverter implements Converter {
	/**
	 *  
	 */
	public static final String[] DEFAULT_FORMATS = { "yyyy-MM-dd HH:mm:ss",
	        "yyyy-M-d HH:mm:ss", "yyyy-MM-dd H:m:s", "yyyy-M-d H:m:s",
	        "yyyy/MM/dd HH:mm:ss", "yyyy-M-d HH:mm:ss", "yyyy/MM/dd H:m:s",
	        "yyyy/M/d H:m:s", "HH:mm:ss", "H:m:s", "yyyy-MM-dd", "yyyy-M-d",
	        "yyyy/MM/dd", "yyyy/M/d" };
	private SimpleDateFormat sdf = new SimpleDateFormat();
	private List<String> formats;

	public DateConverter() {
		formats = Arrays.asList(DEFAULT_FORMATS);
	}

	public void addFormat(String format) {
		formats.add(format);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Object convert(Class c, Object value) {
		if (value == null) {
			return null;
		}
		if (value instanceof String && value.toString().trim().equals("")) {
			return null;
		}
		if (c == java.util.Date.class) {
			Date v = getDate(value);
			if (v != null) {
				return v;
			}
		}
		else if (c == java.sql.Date.class) {
			Date v = getDate(value);
			if (v != null) {
				return new java.sql.Date(v.getTime());
			}
		}
		else if (c == java.sql.Time.class) {
			Date v = getDate(value);
			if (v != null) {
				return new java.sql.Time(v.getTime());
			}
		}
		else if (c == java.sql.Timestamp.class) {
			Date v = getDate(value);
			if (v != null) {
				return new java.sql.Timestamp(v.getTime());
			}
		}
		return value;
	}

	public Date getDate(Object value) {
		for (int i = 0; i < formats.size(); i++) {
			Date v = tryConvert(value, formats.get(i));
			if (v != null) {
				return v;
			}
		}
		return null;
	}

	public Date tryConvert(Object value, String format) {
		try {
			sdf.applyPattern(format);
			Date v = sdf.parse(String.valueOf(value));
			return v;
		}
		catch (Exception e) {
			return null;
		}
	}
}