Problem description

When the console submits the Date string to the background, it is transmitted as a string. If the background receives the Date type, an exception is reported indicating format conversion errors.

To solve

A:

Add the @DateTimeFormat(pattern = “YYYY-MM-DD HH: MM: SS”) annotation to the fields of the entity class.

  • Advantages: Flexibility in defining the type of reception
  • Disadvantages: No global processing, annotations for each field that needs to be converted

Method 2:

Define a BaseController base class that uses the @initBinder annotation to define a global dateformat conversion method that inherits from BaseController when defining other controllers.

@InitBinder
public void initBinder(ServletRequestDataBinder binder) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));        
}
Copy the code
  • Advantages: Global processing without the need to focus on the specific date field to be converted
  • Disadvantages: Only one date format can be defined

Method 3 (recommended) :

Define a DateConverterConfig class that implements the Spring-provided Converter and override the convert() method.

/** * global date format converter */
@Component
public class DateConverterConfig implements Converter<String.Date> {
 
    // Date format
    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 the date *@paramDateStr String Date of a character *@paramFormat String Format *@returnThe 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) {
 
        }
        returndate; }}Copy the code
  • Advantages: High flexibility. You can define any date format in a static code block and then configure the corresponding regular expression, taking into account the first two methods.