博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
初始化参数绑定——日期格式
阅读量:6948 次
发布时间:2019-06-27

本文共 3576 字,大约阅读时间需要 11 分钟。

单种日期格式转换 (yyyy-MM-dd)

 

使用@InitBinder注解实现日期格式转换

 

package cn.happy.controller;import java.beans.PropertyEditor;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Date;import org.springframework.beans.propertyeditors.CustomDateEditor;import org.springframework.stereotype.Controller;import org.springframework.web.bind.WebDataBinder;import org.springframework.web.bind.annotation.InitBinder;import org.springframework.web.bind.annotation.RequestMapping;     @Controller    public class FirstController {         /**          * @param binder          */                 @InitBinder        public void initBinder(WebDataBinder binder){            //PropertyEditor            //类  implements  PropertyEditor            binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));        }                         @RequestMapping("/first.do")        //类型转化工作一定是在真正的handler方法执行前执行的。        public String doFirst(Date birthday,int age) throws Exception{            System.out.println(birthday+"===============");            System.out.println(age+"===============");            return "/WELCOME.jsp";        }                    }

 

 

多种日期格式转换

 

使用正则表达式

package cn.happy.controller;import java.sql.Date;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.regex.Pattern;import org.springframework.beans.TypeMismatchException;import org.springframework.beans.propertyeditors.PropertiesEditor;public class MyDateEditor extends PropertiesEditor{     @Override    public void setAsText(String source) throws IllegalArgumentException {               SimpleDateFormat sdf=getDate(source);      //做一个操作      try {        setValue(sdf.parseObject(source));    } catch (ParseException e) {        e.printStackTrace();    }             }    private SimpleDateFormat getDate(String source) {        SimpleDateFormat sdf=null;        if (Pattern.matches("^\\d{4}-\\d{2}-\\d{2}$", source)) {            sdf=new SimpleDateFormat("yyyy-MM-dd");        }else if (Pattern.matches("^\\d{4}/\\d{2}/\\d{2}$", source)) {            sdf=new SimpleDateFormat("yyyy/MM/dd");        }else if (Pattern.matches("^\\d{4}\\d{2}\\d{2}$", source)) {            sdf=new SimpleDateFormat("yyyyMMdd");        }else {            throw new TypeMismatchException("", Date.class);        }                return sdf;    }                    }

 handler部分代码:

package cn.happy.controller;import java.beans.PropertyEditor;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Date;import org.springframework.beans.propertyeditors.CustomDateEditor;import org.springframework.beans.propertyeditors.PropertiesEditor;import org.springframework.stereotype.Controller;import org.springframework.web.bind.WebDataBinder;import org.springframework.web.bind.annotation.InitBinder;import org.springframework.web.bind.annotation.RequestMapping;     @Controller    public class FirstController {         /**          *           * @param binder          */         @InitBinder        public void initBinder(WebDataBinder binder){            binder.registerCustomEditor(Date.class,new MyDateEditor());                }                 @RequestMapping("/first.do")        //类型转化工作一定是在真正的handler方法执行前执行的。        public String doFirst(Date birthday,int age) throws Exception{            System.out.println(birthday+"===============");            System.out.println(age+"===============");            return "/WELCOME.jsp";        }                    }

这样就可以进行多种日期类型转换了!!!!

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/Smile-123/p/6260115.html

你可能感兴趣的文章
有意义的命名
查看>>
POJ——3517
查看>>
POJ 2886
查看>>
shiro登录步骤源码分析
查看>>
Java进阶篇(二)——抽象类、内部类
查看>>
将Redis设置为后台启动
查看>>
为自己的博客园添加右侧目录
查看>>
TypeScript 学习二 表达式和循环
查看>>
CentOS 6.5安装部署Zabbix监控系统
查看>>
Sql server 2008 R2 导出/导入数据报错之无法打开全局共享内存以与性能 DLL 通信
查看>>
*#06# 新购手机 指令测试
查看>>
Microsoft Lookback Adapter添加并设置回环网卡(转)
查看>>
android点滴(24)之android 创建、删除、判断是否存在快捷方式
查看>>
iOS怎么给颜色设置透明度?
查看>>
开源GIT仓库-----gogs
查看>>
openal-soft
查看>>
数据绑定介绍
查看>>
语言发展历史(1950-2020)
查看>>
使用jar打war包或解压war包
查看>>
毕业论文—使用js将canvas保存为图片文件,并且自定义文件名
查看>>