springboot⽇期数据格式问题:实现
DateToStringStringToDate转换⽅法配置在Application.Java启动类(Spring boot、Spring cloud):
使⽤FastJson
/**
* 编辑fastJson.<br/>
*
* @return HttpMessageConverters
* @author yunchao.li
*/
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters(){
/
/1、先定义⼀个convert转换消息的对象
FastJsonHttpMessageConverter fastConverter=new FastJsonHttpMessageConverter();
//2、添加fastjson的配置信息,⽐如是否要格式化返回的json数据;
FastJsonConfig fastJsonConfig=new FastJsonConfig();
SerializerFeature[] serializerFeatures = new SerializerFeature[]{
//  输出key是包含双引号
//                SerializerFeature.QuoteFieldNames,
//  是否输出为null的字段,若为null 则显⽰该字段
//                SerializerFeature.WriteMapNullValue,
//  数值字段如果为null,则输出为0
//                SerializerFeature.WriteNullNumberAsZero,
/
/  List字段如果为null,输出为[],⽽⾮null
SerializerFeature.WriteNullListAsEmpty,
//  字符类型字段如果为null,输出为"",⽽⾮null
SerializerFeature.WriteNullStringAsEmpty,
//  Boolean字段如果为null,输出为false,⽽⾮null
SerializerFeature.WriteNullBooleanAsFalse,
//  Date的⽇期转换器
SerializerFeature.WriteDateUseDateFormat,
//  循环引⽤
SerializerFeature.DisableCircularReferenceDetect,
};
fastJsonConfig.setSerializerFeatures(serializerFeatures);
//附加:处理中⽂乱码
List<MediaType> fastMedisTypes = new ArrayList<MediaType>();
fastMedisTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMedisTypes);
//3、在convert中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter=fastConverter;
return new HttpMessageConverters(converter);
}
追加StringToDateConverter转换器
package com.verter;
SimpleDateFormat;
import java.util.Date;
import org.apachemons.lang3.StringUtils;
import verter.Converter;
/**
* ClassName:StringToDateConverter <br/>
* Reason:  ⼊参字符串⽇期转换器 . <br/>
*
* @author  yunchao.li
* Date:    2018年4⽉9⽇ 下午2:42:15 <br/>
string转date的方法
* @version  1.0.0 <br/>
*/
public class StringToDateConverter implements Converter<String, Date> {
/**
* dateFormat:⽇期时间格式.
*/
private static final String dateFormat      = "yyyy-MM-dd HH:mm:ss";
/**
* shortDateFormat:⽇期格式.
*/
private static final String shortDateFormat = "yyyy-MM-dd";
/
**
* 字符串转换为⽇期.
* @see verter.Converter#convert(java.lang.Object)      */
@Override
public Date convert(String source) {
if (StringUtils.isBlank(source)) {
return null;
}
source = im();
try {
if (ains("-")) {
SimpleDateFormat formatter;
if (ains(":")) {
formatter = new SimpleDateFormat(dateFormat);
} else {
formatter = new SimpleDateFormat(shortDateFormat);
}
Date dtDate = formatter.parse(source);
return dtDate;
} else if (source.matches("^\\d+$")) {
Long lDate = new Long(source);
return new Date(lDate);
}
} catch (Exception e) {
throw new RuntimeException(String.format("parser %s to Date fail", source));
}
throw new RuntimeException(String.format("parser %s to Date fail", source));
}
}
追加Configuration
package com.figuration;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import t.annotation.Configuration;
import onvert.support.GenericConversionService;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.hod.annotation.RequestMappingHandlerAdapter; import com.verter.StringToDateConverter;
/**
* ClassName:WebConfigBeans <br/>
* Reason:  TODO . <br/>
*
* @author  yunchao.li
* Date:    2018年4⽉9⽇ 下午2:44:04 <br/>
* @version  1.0.0 <br/>
*/
@Configuration
public class WebDateConverterConfiguration {
@Autowired
private RequestMappingHandlerAdapter handlerAdapter;
/**
* 增加字符串转⽇期的功能
*/
@PostConstruct
public void initEditableValidation() {
ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) handlerAdapter            .getWebBindingInitializer();
if (ConversionService() != null) {
GenericConversionService genericConversionService = (GenericConversionService) initializer                .getConversionService();
genericConversionService.addConverter(new StringToDateConverter());
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。