Home > Java > javaTutorial > How to configure the global date type converter in spring boot (code)

How to configure the global date type converter in spring boot (code)

不言
Release: 2018-09-14 16:28:48
Original
2829 people have browsed it

The content of this article is about how to configure the global date type converter (code) in spring boot. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. First customize a type converter

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;
    }
}
Copy after login

2. Register the custom type converter in the configuration class

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());//添加自定义的类型转换器
        }
    }


}
Copy after login

Related recommendations:

mysql Query int type date and convert it to datetime type

The above is the detailed content of How to configure the global date type converter in spring boot (code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template