Springboot Receive front-end date inaccurate problem

Source: Internet
Author: User
Tags locale

Recently encountered a problem, database field of datetime, front-end form form directly after the display of the storage, the date of their own changes, or the date increased by one day, or the hour increased by two hours.

The parameters obtained through Request.getparamater ("CreateDate") are not equal to the parameters that springboot themselves encapsulated CreateDate (Java date type).

Request.getparamater ("CreateDate") gets the same value as the front end, and is correct. Therefore, an error occurred when the problem was positioned to convert to the date type for the Springboot parameter.

Workaround: Customize a parameter Date converter

Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;

Import Java.util.Locale;
Import Org.springframework.context.annotation.Bean;
Import org.springframework.context.annotation.Configuration;

Import Org.springframework.core.convert.converter.Converter; /** * Custom Incoming Date Converter * To resolve front-end submission time data, background date field receive conversion inaccurate problem/@Configuration public class Dateconverconfig {@Bean public Con Verter<string, date> Stringdateconvert () {Return to New converter<string, date> () {@Override public Dat E convert (string source) {//yyyy-mm-dd HH:mm:ss date string converted to date type SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-m
				M-dd HH:mm:ss ");
				Date date = null;
				try {date = Sdf.parse ((String) source);
					The catch (Exception e) {//yyyy-mm-dd date string is converted to the date type SDF = new SimpleDateFormat ("Yyyy-mm-dd");
					try {date = Sdf.parse ((String) source); } catch (ParseException E1) {//eee MMM dd HH:mm:ss Z yyyy string converted to date type SDF =New SimpleDateFormat ("EEE MMM dd HH:mm:ss Z yyyy", locale.uk);
						try {date = Sdf.parse ((String) source);
						catch (parseexception E2) {e2.printstacktrace ();
			}} return date;
	}
		}; }

}

This will solve the problem of date change.

If there is a better solution, or if there is something wrong with this article, please criticize it.

"Sichuan Leshan Programmer's Alliance, welcome everybody to add Group Mutual Exchange Study 5 7 1 8 1 4 7 4 3"


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.