spring boot 配置全局日期类型转换器
1. 首先自定义一个类型转换器
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component; import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date; @Component
public class MyDataConvert implements Converter<String, Date> {
//这里可以自己灵活变通
private String [] pattern = {"yyyy-MM-dd","yyyy年MM月dd日"};
@Override
public Date convert(String s) {
System.out.println("convert");
for (int i=0;i<pattern.length;i++){
try {
return new SimpleDateFormat(pattern[i]).parse(s);
} catch (ParseException e) {
continue;
}
}
return null;
}
}
2.在配置类中注册自定义类型转换器
import com.example.demo.convert.MyDataConvert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; import javax.annotation.PostConstruct; @Configuration
public class WebConfigurer extends WebMvcConfigurationSupport { @Autowired
private RequestMappingHandlerAdapter handlerAdapter; @PostConstruct
public void initEditableAvlidation() { ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer)handlerAdapter.getWebBindingInitializer();
if(initializer.getConversionService()!=null) {
GenericConversionService genericConversionService = (GenericConversionService)initializer.getConversionService();
genericConversionService.addConverter(new MyDataConvert());//添加自定义的类型转换器
}
} }
spring boot 配置全局日期类型转换器的更多相关文章
- struts2中配置全局日期类型转换器
1.编写一个类,继承StrutsTypeConverter,实现其中的convertFromString和convertToString方法,该类如下: package me.edu.utils; i ...
- Spring Boot配置全局异常捕获
1 SpringBoot配置全局的异常捕获 项目的说明 配置thymeleaf作为视图模板 ExceptionController.java模拟测试用 MyAjaxExceptionHandler.j ...
- SpringMVC配置全局日期转换器,处理日期转换异常
Spring 3.1.1使用Mvc配置全局日期转换器,处理日期转换异常链接地址: https://www.2cto.com/kf/201308/236837.html spring3.0配置日期转换可 ...
- Spring MVC__自定义日期类型转换器
WEB层采用Spring MVC框架,将查询到的数据传递给APP端或客户端,这没啥,但是坑的是实体类中有日期类型的属性,但是你必须提前格式化好之后返回给它们.说真的,以前真没这样做过,之前都是一口气查 ...
- 【spring boot】12.spring boot对多种不同类型数据库,多数据源配置使用
2天时间,终于把spring boot下配置连接多种不同类型数据库,配置多数据源实现! ======================================================== ...
- Spring Boot配置,读取配置文件
Spring Boot配置,读取配置文件 一.配置Spring Boot 1.1 服务器配置 1.2 使用其他Web服务器 1.3 配置启动信息 1.4 配置浏览器显示ico 1.5 Yaml语法 1 ...
- Spring Boot 配置_yaml语法介绍 day02
一.Spring Boot 的全局配置文件(application.properties.application.yml) 1.这两种配置文件是SpringBoot 支持的对默认配置修改的格式.命名和 ...
- Spring Boot 配置优先级顺序
一般在一个项目中,总是会有好多个环境.比如: 开发环境 -> 测试环境 -> 预发布环境 -> 生产环境 每个环境上的配置文件总是不一样的,甚至开发环境中每个开发者的环境可能也会有一 ...
- SSM-SpringMVC-28:SpringMVC类型转换之自定义日期类型转换器
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 例子很简易,要明白的是思路,话不多说,开讲 上篇博客不是说springmvc默认的日期转换格式是yyyy/M ...
随机推荐
- Java Math类学习
1. java.lang.Math Math类其成员皆为静态成员(static),无需创建对象,直接用类名Math作为前缀使用它们即可. 2. Math类有两个静态常量:E(自然对数)和PI(圆周 ...
- 1503. [NOI2004]郁闷的出纳员【平衡树-splay】
Description OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的 工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经 ...
- spring-mybatis项目搭建(支持多数据源)
一.目录结构图 2.配置文件内容 db.properties: #oracle public oracle.driverClass=oracle.jdbc.driver.OracleDriver or ...
- NSPredicate的使用
简述 NSPredicate谓词条件过滤器,一般用于过滤数组数据,原理和用法都类似于SQL中的where,作用相当于数据库的过滤取. 常用函数 创建谓词 + (NSPredicate *)predic ...
- P2983 [USACO10FEB]购买巧克力Chocolate Buying
题目描述 Bessie and the herd love chocolate so Farmer John is buying them some. The Bovine Chocolate Sto ...
- 配虚拟ip脚本
cat /home/master/init_pandora.sh #! /bin/shuser=`whoami`if [ $user = 'master' ]then sudo /sbin/ifcon ...
- 学习java前端 两种form表单提交方式
第一种:原生方式 注意点:button标签的style为submit <form action="/trans/doTrans.do" method="post&q ...
- 各国货币json文件
[ {"countryname":"","name":"请选择","currency":" ...
- Echarts根据数据长度变换柱状图柱状的颜色
//查询图表数据 function GetData() { var qs = $("#qs").val(); ...
- 关于HBase Shell命令基本操作示例
HBase 为用户提供了一个非常方便的使用方式, 我们称之为“HBase Shell”. HBase Shell 提供了大多数的 HBase 命令, 通过 HBase Shell 用户可以方便地创建. ...