Problem restore: The current table submitted date format data to the background save, has been done as a string transmission, if the background is the data type acceptance will report 400 format error. This is the time to deal with:
The No. 0 (most low): The background is received with a String type field, if necessary, and replaced with date.
1th: Using @datetimeformat (pattern = "Yyyy-mm-dd HH:mm:ss") annotation on the Entity field,
The advantage of this approach is that you can flexibly define the type of reception
The disadvantage is obvious: cannot be globally unified processing, need to add annotation for each need to convert the field too troublesome
2nd: Write a basecontroller, each need to deal with the controller inherit this basecontroller, in Basecontroller use @initbinder write a global conversion date method:
@InitBinder public
void Initbinder (Servletrequestdatabinder binder) {
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
Binder.registercustomeditor (Date.class, New Customdateeditor (SDF, True));
}
The advantage of this approach is that it can be globally unified, without paying attention to the date fields that need to be converted
The disadvantage is: only a date type can be defined, I define "YYYY-MM-DD" can not define "Yyyy-mm-dd HH:mm:ss",
If my front desk different pages come over the date format is not the same.
3rd (enlarged): Custom Dateconverterconfig Implement the converter provided by spring and rewrite the Convert method inside:
/** * Global Handler pre-date uniform processing * @author Zhanghang * @date 2018/1/11/@Component public class Dateconverterconfig implements
Converter<string, date> {private static final list<string> formarts = new arraylist<> (4);
static{Formarts.add ("yyyy-mm");
Formarts.add ("Yyyy-mm-dd");
Formarts.add ("Yyyy-mm-dd hh:mm");
Formarts.add ("Yyyy-mm-dd hh:mm:ss");
@Override public Date Convert (string source) {String value = Source.trim ();
if ("". Equals (value)) {return null;
} if (Source.matches ("^\\d{4}-\\d{1,2}$")) {return parsedate (source, Formarts.get (0));
}else if (source.matches ("^\\d{4}-\\d{1,2}-\\d{1,2}$")) {return parsedate (source, Formarts.get (1)); }else if (Source.matches ("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")) {return parsedate (source, Formarts
. Get (2)); }else if (Source.matches ("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$ "))
{return parsedate (source, Formarts.get (3));
}else {throw new IllegalArgumentException ("Invalid boolean value" + Source + "");
}/** * Format date * @param datestr string Character date * @param format string * @return Date Date
*/Public Date parsedate (string datestr, string format) {date date=null;
try {dateformat DateFormat = new SimpleDateFormat (format);
Date = Dateformat.parse (DATESTR);
catch (Exception e) {} return date; }
}
I'm a springboot project. This class is hosted by the spring container through the @component annotation, and if the SPRINGMVC project also needs to register with the XML configuration file
The advantages are obvious: flexible enough, In the static code block to customize any format date, in the rewrite method in the corresponding regular expression on the line, can also achieve global unified treatment, taking into account the 1th and the second, perfect 666.