xwork-conversion.properties

阅读更多

java.util.Date=com.utils.converter.DateConverter



今天在处理国际化 的时候出现时间类型不能转换。。
网上找了 下 需要


package com.utils.converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import ognl.DefaultTypeConverter;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
     
     
     
     
public class DateConverter extends DefaultTypeConverter {     
     
     
    private static final String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";     
     
    private static final String DATE_PATTERN = "yyyy-MM-dd";     
         
    private static final String MONTH_PATTERN = "yyyy-MM";     
     
    @SuppressWarnings("rawtypes")
	public Object convertValue(Map ognlContext, Object value, Class toType) {     
            Object result = null;     
            if (toType == Date.class) {     
                    try {     
                        result = doConvertToDate(value);     
                    } catch (ParseException e) {     
                        e.printStackTrace();     
                    }     
            } else if (toType == String.class) {     
                    result = doConvertToString(value);     
            }     
            return result;     
    }     
     
    /**
     * String to Date
     * @param value
     * @return
     * @throws ParseException
     */
    private Date doConvertToDate(Object value) throws ParseException {     
            Date result = null;     
     
            if (value instanceof String) {     
                    result = DateUtils.parseDate((String) value, new String[] { DATE_PATTERN, DATETIME_PATTERN, MONTH_PATTERN });     
     
                    if (result == null && StringUtils.isNotEmpty((String)value)) {     
     
                            try {     
                                    result = new Date(new Long((String) value).longValue());     
                            } catch (Exception e) {     
                                    e.printStackTrace();     
                            }     
     
                    }     
     
            } else if (value instanceof Object[]) {     

            	Object[] array = (Object[]) value;     
                    if ((array != null) && (array.length >= 1)) {     
                            value = array[0];     
                            result = doConvertToDate(value);     
                    }     
     
            } else if (Date.class.isAssignableFrom(value.getClass())) {     
                    result = (Date) value;     
            }     
            return result;     
    }     
     
    /**
     * Date to String    
     * @param value
     * @return
     */
    private String doConvertToString(Object value) {     
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATETIME_PATTERN);     
            String result = null;     
            if (value instanceof Date) {     
                    result = simpleDateFormat.format(value);     
            }     
            return result;     
    }     
}


你可能感兴趣的:(xwork-conversion.properties)