/** * 自定义 数据转换器 * @return */ @Bean public ConversionServiceFactoryBean conversionService(){ ConversionServiceFactoryBean conversionServiceFactoryBean=new ConversionServiceFactoryBean(); Set<Converter> converters=new HashSet<Converter>(); converters.add(new DateConverter());//日期转换器 conversionServiceFactoryBean.setConverters(converters); return conversionServiceFactoryBean; }
package cn.freshz.common; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.springframework.core.convert.converter.Converter; /** * SpringMVC 自定义 日期转换器 * * @author :<a href="mailto:zyj@freshz.cn">章英杰</a> * @date :2016-09-05 14:03:54 */ public class DateConverter implements Converter<String, Date> { /** * 可格式化 的日期 字串 */ private static final List<String> formarts = new ArrayList(); 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 = (Date) dateFormat.parse(dateStr); } catch (Exception e) { } return date; } public static void main(String[] args) { System.err.println(new DateConverter().convert("2014-04")); } }
相关推荐
SpringBoot中自定义消息转换器示例源码
在SpringBoot 项目中实现枚举转换,需要创建BaseEnumTypeHandler 类继承BaseEnum 和 BaseTypeHandler 等相关类。想要详细了解如何实现枚举转换的话,那你快点下载后深入理解吧!
主要介绍了springboot全局日期格式化的两种方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
主要介绍了springboot日期转换器实现实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要介绍了Springboot日期转换器实现代码及示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Springboot驼峰下划线转换配置
SpringBoot中自定义拦截器示例代码SpringBoot中自定义拦截器示例代码
该压缩包实现了利用过滤器或者拦截器对登录信息进行验证跳转登陆页的功能,利用的是SpringBoot和thymeleaf,使用前请先看使用说明
Springboot DTO字符字段与日期字段的转换问题 不会自动转换string与date 主要是这个意思,前端提交的JSON里,日期是一个字符串,而对应后端的实体里,它是一个Date的日期,这两个在默认情况下是不能自动转换的,...
springboot实现消息转换、切面AOP和Redis缓存,实现相关技术的实例,以及相关方法的使用说明
Springboot中使用过滤器映射访问路径,并对请求地址进行重定向。
idea软件。SpringBoot的拦截器的博客所写的例子。preHandle()方法的返回值true和false的详细区别还未描述
根据网上的资料总结的一个springboot 转换pdf Word文档大小最好2m以下 需要安装OpenOffice.org 3.3 链接:https://pan.baidu.com/s/1onrkhBCNlGLEmf3hPwzXWw 密码:8h5a
springboot利用拦截器打印各个接口的响应时间,快速统计各接口执行速度,方便接口优化。springboot利用拦截器打印各个接口的响应时间,快速统计各接口执行速度,方便接口优化。springboot利用拦截器打印各个接口的响应...
idea开发软件,实现SpringBoot项目的过滤器。写了博客,直接把代码也上传吧。比较简单,大家不要鄙视,为什么他要写50个字啊。
springboot spring aop 拦截器 注解方式实现脱敏(涉及到:pom.xml -->application.properties --->启动类-->拦截器)
SpringBoot整合Mybatis完整详细版含注册、登录、拦截器配置功能
基于flot的折线图。X轴为日期,Y轴为具体数据。 通过ajax请求定义x轴,y轴的数值。 框架:springboot+mybatis+Flot
springboot 整合flowable-modeler 设计器
springboot集成富文本编辑器,maven项目,themyleaf模板