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"