springmvc全局日期的转换

来源:互联网 发布:淘宝店铺logo图片尺寸 编辑:程序博客网 时间:2024/05/09 05:11

一.接受日期转换(全局)

1.创建一个类实现Converter接口(第一种方式)
import org.springframework.core.convert.converter.Converter;public class DateConverter implements Converter<String, Date>{     @Override     public Date convert(String source) {          SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");               //指定日期/时间解析是否不严格          dateFormat.setLenient(false);                   try {                      try {                        return dateFormat.parse(source);                   } catch (java.text.ParseException e) {                        e.printStackTrace();                   }                  } catch (ParseException e) {                       e.printStackTrace();                  }                         return null;       }}
2.在springmvc.xml进行配置
<mvc:annotation-driven conversion-service="conversionService" /><!--全局时间转换器  -->     <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">           <property name="converters">               <list>                   <bean class="com.bet.content.entity.DateConverter" />               </list>           </property>  

注解方式(不是全局)(第二种方式)

在pojo类的属性上加上注解,就可以转换页面传过来的时间
@DateTimeFormat(pattern="yyyy-MM-ddHH:mm:ss") 
 private Date receiveAppTime;

在Controller层加入一段数据代码(第三种方式)

@InitBinder  public void initBinder(WebDataBinder binder) {  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  dateFormat.setLenient(false);  binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));   //true:允许输入空值,false:不能为


二,接受多种日期转换

public class DateConverter implements Converter<String, Date>{//添加不同的日期格式private static  List<String> formats = new ArrayList<String>();    static{    formats.add("yyyy");    formats.add("yyyy-MM");    formats.add("yyyy-MM-dd");    formats.add("yyyy-MM-dd HH:mm");    formats.add("yyyy-MM-dd HH:mm:ss");    formats.add("yyyy/MM");    formats.add("yyyy/MM/dd");    formats.add("yyyy/MM/dd HH:mm");    formats.add("yyyy/MM/dd HH:mm:ss");    }@Overridepublic Date convert(String source) {try {if (source.matches("^\\d{4}$")) {//2017return parseDate(source, formats.get(0));} else if (source.matches("^\\d{4}-\\d{1,2}$")) {//2017-09return parseDate(source, formats.get(1));} else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")) {//2017-09-10return parseDate(source, formats.get(2));} else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")) {//2017-09-10 21:15return parseDate(source, formats.get(3));} else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {//2017-09-10 21:15:30return parseDate(source, formats.get(4));} else if (source.matches("^\\d{4}/\\d{1,2}$")) {//2017/09return parseDate(source, formats.get(5));} else if (source.matches("^\\d{4}/\\d{1,2}/\\d{1,2}$")) {//2017/09/10return parseDate(source, formats.get(6));} else if (source.matches("^\\d{4}/\\d{1,2}/\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")) {//2017/09/10 21:15return parseDate(source, formats.get(7));} else if (source.matches("^\\d{4}/\\d{1,2}/\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {//2017/09/10 21:15:30return parseDate(source, formats.get(8));} else { throw new BetException("在springmvc自定义全局日期转换器,没有相对应的日期格式与传入的日期相匹配!!!");}} catch (Exception e) {e.printStackTrace();}return null;} /**     * 功能描述:格式化日期     *      * @param dateStr     *            String 字符型日期     * @param format     *            String 格式     * @return Date 日期     */    public  Date parseDate(String dateStr, String format) {        Date date=null;        SimpleDateFormat dateFormat = new SimpleDateFormat(format);        dateFormat.setLenient(false);//指定日期/时间解析为不严格            try {date = (Date) dateFormat.parse(dateStr);} catch (ParseException e) {e.printStackTrace();} catch (Exception e1) {e1.printStackTrace();}        return date;    }}