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 ...
随机推荐
- 洛谷P4494 [HAOI2018]反色游戏(tarjan)
题面 传送门 题解 我们先来考虑一个联通块,这些关系显然可以写成一个异或方程组的形式,形如\(\oplus_{e\in edge_u}x_e=col_u\) 如果这个联通块的黑色点个数为奇数,那么显然 ...
- ueditor的bug
1)过滤script style标签ueditor.all.jsallowDivTransToP:false ueditor.config.js,allowDivTransToP:false //允许 ...
- Python里面的负号的各种神奇用法?来填坑啦
1.x.reshape(-1,2) x = np.linspace(1,10,10) x.reshape(-1,2) reshape(-1,2)里-1的应该是不管多少行,按两列算,行数自动算出.同理, ...
- 命令行里打 cd 简直是浪费生命
简评:作为工程师,你在命令行下最常打的命令无非就是 cd 与 ls.这些年你浪费了多少时间? 作为一个程序员或者在 shell 中花费大量时间的人,你可能会经常以一种低效率的方式在目录中来回移动,特别 ...
- 【洛谷2416】泡芙(Tarjan+LCA)
题目描述 火星猫经过一番努力终于到达了冥王星.他发现冥王星有 \(N\) 座城市,\(M\) 条无向边.火星猫准备出发去找冥王兔,他听说有若干泡芙掉落在一些边上,他准备采集一些去送给冥王兔.但是火星猫 ...
- SDUT OJ 数据结构实验之图论四:迷宫探索
数据结构实验之图论四:迷宫探索 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...
- winform工具1-图片去除水印
效果图: 思路: 1.获取图片 2.处理水印 3.保存处理的图片 代码: 获取图片: private void button1_Click(object sender, EventArgs e) { ...
- css样式继承经验记录
与元素(文字颜色.字体等)相关的样式默认会被继承: 与元素在页面上的布局相关的样式默认不会被继承: <body> <p>I like <span>aplles< ...
- 从fis中得来的数据结构,Object版,
/* * config * caoke */ 'use strict'; //You can't use merge in util.js function merge(source, target) ...
- contentType 应用,(表中数据大量存在外键时使用)
当表中有大量的foreignkey 外键时,而且后期还有可能加入一些外键时, 使用django自带的contenttype表(表结构是app名和表的对应的每个类字段) 1. Django 中的 con ...