SpringBoot全局时间转换器
SpringBoot全局时间转换器
日常开发中,接收时间类型参数处处可见,但是针对不同的接口。往往需要的时间类型不一致
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")需要每个属性上都要增加,大大增加工作量
可通过实现Converter接口,进行全局时间转换
全局时间转换代码
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* 非requestbody方式接收date参数转换器
* requestbody形式,采用json序列化进行配置格式
* 本转换器只接收-为间隔。统一返回给前端的也以-为间隔
* 若新增类型,可增加formats与正则匹配进行转换
*/
@Component
public class DateConverterConfig implements Converter<String, Date> {
private static final List<String> formarts = new ArrayList<>(4);
/**
* 支持的格式
* 既然已经使用了转换器,就不使用yyyy/MM等形式了。
* 全局均使用yyyy-MM形式
*/
static {
formarts.add("yyyy-MM");
formarts.add("yyyy-MM-dd");
formarts.add("yyyy-MM-dd HH:mm");
formarts.add("yyyy-MM-dd HH:mm:ss");
}
@Override
public Date convert(String source) {
String value = source.trim();
if ("".equals(value)) {
return null;
}
if (source.matches("^\\d{4}-\\d{1,2}$")) {
return parseDate(source, formarts.get(0));
} else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")) {
return parseDate(source, formarts.get(1));
} else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")) {
return parseDate(source, formarts.get(2));
} else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {
return parseDate(source, formarts.get(3));
} else {
throw new IllegalArgumentException("Invalid boolean value '" + source + "'");
}
}
/**
* 格式化日期
*
* @param dateStr String 字符型日期
* @param format String 格式
* @return Date 日期
*/
public Date parseDate(String dateStr, String format) {
Date date = null;
try {
DateFormat dateFormat = new SimpleDateFormat(format);
date = dateFormat.parse(dateStr);
} catch (Exception e) {
}
return date;
}
}
SpringBoot全局时间转换器的更多相关文章
- 3种 Springboot 全局时间格式化方式,别再写重复代码了
本文收录在个人博客:www.chengxy-nds.top,技术资料共享,同进步 时间格式化在项目中使用频率是非常高的,当我们的 API 接口返回结果,需要对其中某一个 date 字段属性进行特殊的格 ...
- SpringBoot中时间格式化的5种方法!
在我们日常工作中,时间格式化是一件经常遇到的事儿,所以本文我们就来盘点一下 Spring Boot 中时间格式化的几种方法. 时间问题演示 为了方便演示,我写了一个简单 Spring Boot 项 ...
- struts局部、全局类型转换器
第01步:编写bean package com.self.bean; import java.util.Date; public class User { private Date birthday ...
- struts2视频学习笔记 13-14(自定义局部和全局类型转换器(转换Date格式))
课时13 自定义类型转换器 局部(对某个action类) package tutorial; import java.util.Date; public class HelloWorld { priv ...
- Struts2之自定义局部类型转换器、全局类型转换器
Struts2自定义类型转换器分为局部类型转换器和全局类型转换器 (1)局部类型转换器 如果页面传来一个参数reg.action?birthday=2010-11-12到后台action,然后属性用 ...
- springboot上传文件 & 不配置虚拟路径访问服务器图片 & springboot配置日期的格式化方式 & Springboot配置日期转换器
1. Springboot上传文件 springboot的文件上传不用配置拦截器,其上传方法与SpringMVC一样 @RequestMapping("/uploadPicture&q ...
- SpringMVC配置全局日期转换器,处理日期转换异常
Spring 3.1.1使用Mvc配置全局日期转换器,处理日期转换异常链接地址: https://www.2cto.com/kf/201308/236837.html spring3.0配置日期转换可 ...
- springboot 全局异常处理
springboot 全局异常处理 研究了半天springboot的全局异常处理,虽然还是需要再多整理一下,但是对于常见的404和500足以可以区分开,能够根据这两个异常分别处理 首先配置视图解析路径 ...
- SpringBoot全局异常拦截
SpringBoot全局异常捕获 使用到的技能 @RestControllerAdvice或(@ControllerAdvice+@ResponseBody) @ExceptionHandler 代码 ...
随机推荐
- HTML 和CSS
1 HTML 介绍1.1 web 服务本质import socket sk = socket.socket() sk.bind(("127.0.0.1", 8080))sk.lis ...
- 数据库原理-事务隔离与多版本并发控制(MVCC)
刚来美团实习,正好是星期天,不得不说,其内部的资料很丰富,看了部分文档后,对数据库事务这块更理解了.数据库事务的ACID,大家都知道,为了维护这些性质,主要是隔离性和一致性,一般使用加锁这种方式.同时 ...
- 转:Http协议中Cookie详细介绍
Http协议中Cookie详细介绍 Cookie总是保存在客户端中,按在客户端中的存储位置,可分为内存Cookie和硬盘Cookie.内存Cookie由浏览器维护,保存在内存中,浏览器关闭后就消失了, ...
- 第11.3节 Python正则表达式搜索支持函数search、match、fullmatch、findall、finditer
一. 概述 re模块的函数search.match.fullmatch.findall.finditer都是用于搜索文本中是否包含指定模式的串,函数的参数都是一样的,第一个参数是模式串.第二个是搜索文 ...
- 第11.15节 Python正则表达式转义符定义的特殊序列
一. 引言 在前面<第11.13节 Python正则表达式的转义符"\"功能介绍>介绍了正则表达式转义符'\',只不过当时作为转义符主要是用于在正则表达式中表示元字符自 ...
- Python中高级知识(非专题部分)学习随笔
Python学习随笔:使用xlwings读取和操作Execl文件 Python学习随笔:使用xlwings新建Execl文件和sheet的方法 博客地址:https://blog.csdn.net/L ...
- dataframe 检查缺失值
s = df.isnull().any() #返回series形式,可以用enumerate打印s #true代表有空值 null_index = [] for i,j in enumerate(s) ...
- 理解 tf.reduce_sum(),以及tensorflow的维axis
易错点:注意带上参数axis,否则的话,默认对全部元素求和,返回一个数值int 参考:https://www.jianshu.com/p/30b40b504bae tf.reduce_sum( inp ...
- selenium 淘宝商品分页
通过这行代码确定每页的下一页,因为从淘宝的第4页 xpath就匹配不出下一页的位置#这是面向对象写法,不用的把self. 去掉即可next_button = self.driver.find_elem ...
- nginx 静态化合集(去掉index.php目录)
访问某域名时,去掉index.php目录时达到效果一样 如:www.test1/index.php/test2跟www.test1/test2效果一致 在vhosts.conf中加重写就可以了 loc ...