一、实现日期格式数据类型的转换
首先,定义DateConverter实现Converter<String, Date>接口:
1 package com.stevlu.common; 2 3 import org.springframework.core.convert.converter.Converter; 4 5 import java.text.ParseException; 6 import java.text.SimpleDateFormat; 7 import java.util.Date; 8 9 /** 10 * Created by Steven Lu on 2018/11/21. 11 */ 12 public class DateConverter implements Converter<String, Date> { 13 private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); 14 15 @Override 16 public Date convert(String s) { 17 if ("".equals(s) || s == null) { 18 return null; 19 } 20 try { 21 return simpleDateFormat.parse(s); 22 } catch (ParseException e) { 23 e.printStackTrace(); 24 } 25 return null; 26 } 27 }