提交 | 用户 | 时间
|
1ac2bc
|
1 |
package cn.stylefeng.guns.config.web; |
懒 |
2 |
|
|
3 |
import cn.hutool.core.date.DateUtil; |
|
4 |
import cn.stylefeng.roses.core.util.ToolUtil; |
|
5 |
import org.springframework.beans.factory.annotation.Autowired; |
|
6 |
import org.springframework.context.annotation.Configuration; |
|
7 |
import org.springframework.core.convert.converter.Converter; |
|
8 |
import org.springframework.core.convert.support.GenericConversionService; |
|
9 |
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer; |
|
10 |
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; |
|
11 |
|
|
12 |
import javax.annotation.PostConstruct; |
|
13 |
import java.util.Date; |
|
14 |
|
|
15 |
/** |
|
16 |
* 默认的string to date的转化 |
|
17 |
* |
|
18 |
* @author fengshuonan |
|
19 |
* @Date 2019/2/12 20:09 |
|
20 |
*/ |
|
21 |
@Configuration |
|
22 |
public class String2DateConfig { |
|
23 |
|
|
24 |
@Autowired |
|
25 |
private RequestMappingHandlerAdapter handlerAdapter; |
|
26 |
|
|
27 |
/** |
|
28 |
* 默认时间转化器 |
|
29 |
*/ |
|
30 |
@PostConstruct |
|
31 |
public void addConversionConfig() { |
|
32 |
ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) handlerAdapter.getWebBindingInitializer(); |
|
33 |
if ((initializer != null ? initializer.getConversionService() : null) != null) { |
|
34 |
GenericConversionService genericConversionService = (GenericConversionService) initializer.getConversionService(); |
|
35 |
genericConversionService.addConverter(new StringToDateConverter()); |
|
36 |
} |
|
37 |
} |
|
38 |
|
|
39 |
/** |
|
40 |
* 时间字符串转date的格式 |
|
41 |
* |
|
42 |
* @author fengshuonan |
|
43 |
* @Date 2019/10/22 13:42 |
|
44 |
*/ |
|
45 |
public static class StringToDateConverter implements Converter<String, Date> { |
|
46 |
@Override |
|
47 |
public Date convert(String dateString) { |
|
48 |
if (ToolUtil.isEmpty(dateString)) { |
|
49 |
return null; |
|
50 |
} else { |
|
51 |
return DateUtil.parse(dateString); |
|
52 |
} |
|
53 |
} |
|
54 |
} |
|
55 |
|
|
56 |
} |