zoukankan      html  css  js  c++  java
  • springmvc自定义日期编辑器

    1.控制器

    @Controller
    public class MyController {
    
        // 处理器方法
        @RequestMapping(value = "/first.do")
        public String doFirst(Date birthday, int age) {
            return "/jsp/two.jsp";
        }
    
        // 自定义一个方法
        @InitBinder
        public void initBinder(WebDataBinder binder) {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            binder.registerCustomEditor(Date.class, new MyDateEdit());
        }
    }

    2.自定义日期编辑器

    public class MyDateEdit extends PropertiesEditor {
    
        @Override
        public void setAsText(String source) throws IllegalArgumentException {
            SimpleDateFormat sdf = getDateFromte(source);
            try {
                Date date = sdf.parse(source);
                setValue(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    
        private SimpleDateFormat getDateFromte(String source) {
    
            SimpleDateFormat sdf = new SimpleDateFormat();
    
            if (Pattern.matches("^\d{4}-\d{2}-\d{2}$", source)) {
                sdf = new SimpleDateFormat("yyyy-MM-dd");
            }
            if (Pattern.matches("^\d{4}/\d{2}/\d{2}$", source)) {
                sdf = new SimpleDateFormat("yyyy/MM/dd");
            }
            if (Pattern.matches("^\d{4}\d{2}\d{2}$", source)) {
                sdf = new SimpleDateFormat("yyyyMMdd");
            }
            if (Pattern.matches("^\d{4}年\d{2}月\d{2}日$", source)) {
                sdf = new SimpleDateFormat("yyyy年MM月dd日");
            }
            return sdf;
        }
    
    }
  • 相关阅读:
    eclipse c++
    smb
    osx mount nfs/smb
    0927用的
    0926 隐藏地址栏
    0921笔记
    生成文件并下载
    在线图标
    react redux
    electron
  • 原文地址:https://www.cnblogs.com/cnsdhzzl/p/6097888.html
Copyright © 2011-2022 走看看