首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Struts2 DateTypeConverter变换类

2012-12-26 
Struts2 DateTypeConverter转换类?package com.msd.payCenter.web.converterimport java.text.ParseExcep

Struts2 DateTypeConverter转换类

?

package com.msd.payCenter.web.converter;import java.text.ParseException;import java.text.SimpleDateFormat;  import java.util.Date;  import java.util.HashMap;import java.util.Map;  import java.util.Map.Entry;import org.apache.commons.lang.StringUtils;import com.msd.payCenter.commons.util.RegexUtils;import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;  /** * 日期转换器 * @author nassir wen * @date 2011-12-13 */public class DateTypeConverter extends DefaultTypeConverter {    /*转换器配置:xwork-conversion.properties     *      * java.util.Date=xxx.xxx.xxxx.DateTypeConverter     *      * */        private static Map<String,SimpleDateFormat> DATE_FORMAT_MAP = new HashMap<String, SimpleDateFormat>();        static {        DATE_FORMAT_MAP.put("\\d{1,2}/\\d{1,2}/\\d{4}", new SimpleDateFormat("dd/MM/yyyy"));        DATE_FORMAT_MAP.put("\\d{8}", new SimpleDateFormat("yyyyMMdd"));        DATE_FORMAT_MAP.put("\\d{4}-\\d{1,2}-\\d{1,2}", new SimpleDateFormat("yyyy-MM-dd"));        DATE_FORMAT_MAP.put("\\d{4}/\\d{1,2}/\\d{1,2}", new SimpleDateFormat("yyyy/MM/dd"));        DATE_FORMAT_MAP.put("\\d{4}-\\d{1,2}-\\d{1,2} \\d[0-23]:\\d[0-59]:\\d[0-59]",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));    }        @SuppressWarnings("rawtypes")    @Override       public Object convertValue(Map context, Object value, Class toType) {           if (toType == Date.class) {  //浏览器向服务器提交时,进行String to Date的转换            String dateString = null;               String[] params = (String[])value;            dateString = params[0];//获取日期的字符串                        if(StringUtils.isEmpty(dateString)) {                return null;            }                        SimpleDateFormat sdf = null;                        for(Entry<String, SimpleDateFormat> entry: DATE_FORMAT_MAP.entrySet()){                String key = entry.getKey();                if(RegexUtils.isMatch(dateString, key)) {                    sdf = entry.getValue();                    break;                }            }                        if(sdf != null) {                try {                    return sdf.parse(dateString);                } catch (ParseException e) {                    e.printStackTrace();                }            }else {                System.err.println("value : " +dateString + " not found dateFormat type ");            }                        return null;        }//        else if (toType == String.class) {   //服务器向浏览器输出时,进行Date to String的类型转换//            Date date = (Date)value;   //            return new SimpleDateFormat("yyyy-MM-dd").format(date);//输出的格式是yyyy-MM-dd   //        }        return null;       }        public static void main(String[] args) throws ParseException {        String dateString = "2012-12-12 12:12:12";        SimpleDateFormat sdf = null;        for(Entry<String, SimpleDateFormat> entry: DATE_FORMAT_MAP.entrySet()){            String key = entry.getKey();            if(RegexUtils.isMatch(dateString, key)) {                sdf = entry.getValue();                break;            }        }        System.out.println(sdf.parse(dateString));    }} 

?

public class RegexUtils {    /**     * 是否匹配     *      * @param str     * @param regex     * @return     */    public static boolean isMatch(String str, String regex) {        if (StringUtils.isEmpty(str) || StringUtils.isEmpty(regex)) {            return false;        }        return StringUtils.isNotEmpty(match(str, regex)) ? true : false;    }    /**     * 过滤原字符     *      * @param str     * @param regex     * @return     */    public static String filter(String str, String regex) {        if (StringUtils.isEmpty(str)) {            return "";        }        if (StringUtils.isEmpty(regex)) {            return str;        }        Pattern p = Pattern.compile(regex);        Matcher m = p.matcher(str);        str = m.replaceAll("");        return str;    }    /**     * 匹配原字符     *      * @param str     * @param regex     * @return     */    public static String match(String str, String regex) {        if (StringUtils.isEmpty(str)) {            return "";        }        if (StringUtils.isEmpty(regex)) {            return str;        }        Pattern p = Pattern.compile(regex);        Matcher m = p.matcher(str);        return m.find() ? m.group() : "";    }    /**     * 过滤敏感数据     *      * @param sourceStr      * @param frontSize 前保留位数     * @param backSize 后保留位数     * @return     */    public static String filterSensitiveData(String sourceStr, int frontSize,            int backSize) {        String result = "";        if (StringUtils.isNotEmpty(sourceStr)) {            int len = sourceStr.length();            if (len >= frontSize + backSize) {                result = sourceStr.substring(0, frontSize)                        + (sourceStr.substring(frontSize, sourceStr.length()                                - backSize)).replaceAll(".", "*")                        + sourceStr.substring(sourceStr.length() - backSize);            }else {                result = sourceStr.replaceAll(".", "*");            }        }        return result;    }        public static void main(String[] args) {        System.out.println("aaaaaaaaaaaa");        System.out.println(RegexUtils.filter("(我爱你)bbb", "\\(\\)"));        //过滤前一个括号里的 中文        System.out.println(RegexUtils.filter("(我爱你)bb我b", "\\([\\u4e00-\\u9fa5]+\\)"));            }
?

热点排行