Springmvc/springboot handles three ways to automatically convert foreground string dates to background date types __spring

Source: Internet
Author: User
Tags dateformat string format

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.

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.