Spring Boot Date Conversion

Source: Internet
Author: User

Spring boot serves as a simple service architecture for microservices. has its own characteristics. Quick and easy to build architecture. Here I'm simply introducing two of the issues I've met. The time type passed by the first foreground page cannot be automatically mapped to the Java Date type issue.

A spring container registers the mapping into the container when it is launched. Takes effect as the container starts. Sometimes it's missing the mapping we need, so we need to add a bean to our container to do our own mapping. Details are handled as follows.

@Bean
Public converter<string, Date> Addnewconvert () {
return new converter<string, date> () {
@Override
Public Date convert (String source) {
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
Date date = null;
try {
Date = Sdf.parse ((String) source);
} catch (ParseException e) {
E.printstacktrace ();
}
return date;
}
};
}

Two. We found a 8-hour time difference when using this architecture. It's also a matter of architecture.

Solution Add Spring.jackson.time-zone=gmt+8 in the Application.properties file

If the time data returned from the controller needs to be directly into a fixed string format, you need to add the following configuration in Application.properties

SPRING.JACKSON.DATE-FORMAT=YYYY-MM-DD HH:mm:ss

Let the configuration file load into the spring container when it is started.

Spring Boot Date Conversion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.