initBinder转换日期格式
@Controller
public class FirstController {
@RequestMapping("/first")
//类型转化工作一定是在真正的handler方法执行前执行的
public String doFirst(Date birthday ,int age)throws Exception{
System.out.println(birthday+"=================");
System.out.println(age+"=============");
return "/WELCOME.jsp";
} @ExceptionHandler(TypeMismatchException.class)
public ModelAndView parseException(TypeMismatchException ex, HttpServletRequest request){
System.out.println("hehehehe哈");
ModelAndView mv=new ModelAndView();
mv.addObject("mydate",request.getParameter("birthday"));
mv.setViewName("/typeconverter.jsp");
return mv;
}
public class MyDateEditor extends PropertiesEditor {
@Override
public void setAsText(String source)throws IllegalArgumentException{
SimpleDateFormat sdf=getDateFormate(source);
try {
Date date = sdf.parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
}
private SimpleDateFormat getDateFormate(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;
}
}
这种方式实现 从页面获取到输入的字符串,转换成日期格式,但只作用在单个的controller控制器中
initBinder转换日期格式的更多相关文章
- SQLserver中用convert函数转换日期格式
SQLserver中用convert函数转换日期格式 2008-01-23 15:47 SQLserver中用convert函数转换日期格式2008-01-15 15:51SQLserver中用con ...
- SQLserver中用convert函数转换日期格式(1)
SQLserver中用convert函数转换日期格式2008-01-15 15:51SQLserver中用convert函数转换日期格式 SQL Server中文版的默认的日期字段datetime格式 ...
- 时间戳转换日期格式 - Vue
日常开发中经常会遇到时间相关的问题,服务端返回的数据都是以时间戳的方式,那么需要将其处理转化为对应的时间格式,具体方式如下: 一.filters 中 formatDate 方法实现 <scrip ...
- excel转换日期格式,将yyyymmdd类型日期转换成yyyy-mm-dd等日期类型方法
源数据日期格式:例如: 20160420 20160422 目标日期格式类型: 2016-4-20 2016-4-22 或 2016/04/20 2016/04/22 方法: 一.选中相应数据的单元格 ...
- js时间戳转换日期格式和日期计算
一.时间戳转换日期 function formatDate(datetime) { // 获取年月日时分秒值 slice(-2)过滤掉大于10日期前面的0 var year = datetime.ge ...
- JS中Unix时间戳转换日期格式
<!doctype html> <html> <head> <title>Unix时间戳转换成日期格式</title> <script ...
- java转换日期格式为 RFC1123
import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import java.util. ...
- fastJson 转换日期格式
第一种方法: JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd"; String str = JSON.toJSONString(user,Seria ...
- 【python】统一转换日期格式dateutil.parser.parse
背景: 我有很多很多的日志数据,每个日志里面都有日期字符串,我需要将其转换为datetime格式. 问题是,这些日志里的字符串格式五花八门,有2017-05-25T05:27:30.313292255 ...
随机推荐
- C# winform间窗体传值简单Demo
form1是用来接收值的 using System; using System.Collections.Generic; using System.ComponentModel; using Syst ...
- 静态库(.a)与动态库(.so)的简明介绍
静态库(.a)与动态库(.so)的简明介绍 gcc有很多关于静态库,动态库的选项如-l,-L,-fPIC,-shared -Wl,-soname,看着很复杂容易混淆,其实静态库和动态库都是应需而生,只 ...
- 181114socke编程
一.Socket Families 地址簇 socket.AF_UNIX socket.AF_INET socket.AF_INET6 二.Socker Types socket.SOCK_STREA ...
- jmeter 正则表达式的关联
在工作中,用JM录制了登录---退出的脚本,但是多次请求后发现,总是返回的录制脚本的时候使用的账号的数据. 经过研究发现,login后,响应里的每次返回的token值是变化的,顺着往下看,下一个请求中 ...
- SLAM入门必收藏的资料
搜集了各大网络,请教了SLAM大神,终于把SLAM的入门资料搜集全了!在分享资料前,我们先来看看,SLAM技术入门前需要具备哪些知识?首先学习SLAM需要会C和C++,网上很多代码还用了11标准的C+ ...
- HDU_1028 Ignatius and the Princess III 【母函数的应用之整数拆分】
题目: "Well, it seems the first problem is too easy. I will let you know how foolish you are late ...
- v-model 用在组件中
官方文档: 使用自定义事件的表单输入组件 官方也说明了,v-model只不过是一个语法糖而已,真正的实现靠的还是 1. v-bind : 绑定响应式数据 2. 触发 input 事件 并传递数据 (核 ...
- Oracle "set define off" 关闭替代变量功能
set define off关闭替代变量功能 在SQL*Plus中默认的"&"表示替代变量,也就是说,只要在命令中出现该符号,SQL*Plus就会要你输入替代值.这就意味着 ...
- Spring Annotation(注解)
Spring Boot Annotation @SpringBootApplication 必须作用在main 方法所在类 @RequestMapping @GetMapping @PostMappi ...
- 2.rabbitmq 系列教程
rabbitmq系列教程-文章[转] 视频分享: 链接:https://pan.baidu.com/s/1s_Qr2A1o0s8Ru0exK62jqg 提取码:eb68