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