SpringMVC+FastJson 自定义日期转换器

时间:2025-03-17 20:37:55

来自: /pwh19920920/blog/614066


对于有的时候要输出日期格式为yyyy-MM-dd,而有的时候要输出yyyy-MM-dd hh:mm:ss时怎么办?

第一种方案:纯注解式, 对日期类型的字段进行注解

@JSONField(format = "yyyy-MM-dd")
private Date updateDate;

@JSONField(format = "yyyy-MM-dd hh:mm:ss")
private Date createDate;

public Date getUpdateDate() {
    return updateDate;
}

public void setUpdateDate(Date updateDate) {
     = updateDate;
}

public void setCreateDate(Date createDate) {
     = createDate;
}

public Date getCreateDate() {
    return createDate;
}


第二种方案:使用fastjson的<value>WriteDateUseDateFormat</value>配置(使得返回的日期类型默认为yyyy-MM-dd hh:mm:ss), 特殊类型使用字段@JSONField来进行控制

<!-- 默认的注解映射的支持, -->
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager">
   <mvc:message-converters register-defaults="true">
      <!-- 将Jackson2HttpMessageConverter的默认格式化输出为true -->
      <!-- 配置Fastjson支持 -->
      <bean class="">
         <property name="supportedMediaTypes">
            <list>
               <value>text/html;charset=UTF-8</value>
               <value>application/json</value>
            </list>
         </property>
         <property name="features">
            <list>
               <!-- 输出key时是否使用双引号 -->
               <value>QuoteFieldNames</value>
               <!-- 是否输出值为null的字段 -->
               <!-- <value>WriteMapNullValue</value> -->
               <!-- 数值字段如果为null,输出为0,而非null -->
               <value>WriteNullNumberAsZero</value>
               <!-- List字段如果为null,输出为[],而非null -->
               <value>WriteNullListAsEmpty</value>
               <!-- 字符类型字段如果为null,输出为"",而非null -->
               <value>WriteNullStringAsEmpty</value>
               <!-- Boolean字段如果为null,输出为false,而非null -->
               <value>WriteNullBooleanAsFalse</value>
               <!-- null String不输出  -->
               <value>WriteNullStringAsEmpty</value>
               <!-- null String也要输出  -->
               <!-- <value>WriteMapNullValue</value> -->
               
               <!-- Date的日期转换器 -->
               <value>WriteDateUseDateFormat</value>
            </list>
         </property>
      </bean>
   </mvc:message-converters>
</mvc:annotation-driven>

<!-- REST中根据URL后缀自动判定Content-Type及相应的View -->
<bean  class="">
   <property name="mediaTypes" >
      <map>
         <entry key="json" value="application/json"/>
      </map>
   </property>
   <!-- 这里是否忽略掉accept header,默认就是false -->
   <property name="ignoreAcceptHeader" value="true"/>
   <property name="favorPathExtension" value="true"/>
</bean>
@JSONField(format = "yyyy-MM-dd")
private Date updateDate;

public Date getUpdateDate() {
    return updateDate;
}

public void setUpdateDate(Date updateDate) {
     = updateDate;
}


第三种方案:使用FastJson的消息转换器, 特殊类型使用字段@JSONField来进行控制

import ;
import ;
import ;
import ;
import ;
import ;

import ;
import ;

/**
 * 如果没有注入默认的日期格式,也没有配置<value>WriteDateUseDateFormat</value>, 也没有属性注解@JSONField(format="yyyy-MM-dd hh:mm:ss") 则会转换输出时间戳
 * 如果只配置<value>WriteDateUseDateFormat</value>,则会转换输出yyyy-MM-dd hh:mm:ss
 * 配置<value>WriteDateUseDateFormat</value>, 属性注解@JSONField(format="yyyy-MM-dd hh:mm:ss") 则会转换输出为属性注解的格式
 * 如果注入了默认的日期格式,属性注解@JSONField(format="yyyy-MM-dd hh:mm:ss") 则会转换输出为属性注解的格式
 * 如果注入了默认的日期格式,则会转换输出为默认的日期格式
 * 如果三者都配置则会转换成属性注解的格式
 * Created by PETER on 2016/2/5.
 */
public class CustomerFastJsonHttpMessageConverter extends FastJsonHttpMessageConverter {

    public static SerializeConfig mapping = new SerializeConfig();

    private String defaultDateFormat;

    @Override
    protected void writeInternal(Object obj, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
        OutputStream out = ();
        String text = (obj, mapping, ());
        byte[] bytes = (getCharset());
        (bytes);
    }

    public void setDefaultDateFormat(String defaultDateFormat) {
        (, new SimpleDateFormatSerializer(defaultDateFormat));
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans" 
      xmlns:xsi="http:///2001/XMLSchema-instance"
      xmlns:context="/schema/context" 
      xmlns:dubbo="/schema/dubbo"
      xmlns:mvc="/schema/mvc"
      xsi:schemaLocation="
       /schema/beans /schema/beans/spring-beans-4.
      /schema/context /schema/context/spring-context-4.
      /schema/dubbo /schema/dubbo/
      /schema/mvc /schema/mvc/spring-mvc-4.">

   <description>Spring MVC Configuration</description>

   <!-- 加载配置属性文件 -->
   <context:property-placeholder ignore-unresolvable="true" location="classpath:/" />

   <!-- 扫描dubbo注解需要在controller之前,否则会造成无法注入的问题 -->
   <dubbo:annotation package=""></dubbo:annotation>

   <!-- 使用Annotation自动注册Bean,只扫描@Controller -->
   <context:component-scan base-package="" use-default-filters="false">
      <!-- base-package 如果多个,用“,”分隔 -->
      <context:include-filter type="annotation" expression=""/>
   </context:component-scan>

   <!-- 默认的注解映射的支持, -->
   <mvc:annotation-driven content-negotiation-manager="contentNegotiationManager">
      <mvc:message-converters register-defaults="true">
         <!-- 将Jackson2HttpMessageConverter的默认格式化输出为true -->
         <!-- 配置Fastjson支持 -->
         <bean class="">
            <property name="supportedMediaTypes">
               <list>
                  <value>text/html;charset=UTF-8</value>
                  <value>application/json</value>
               </list>
            </property>
            <property name="features">
               <list>
                  <!-- 输出key时是否使用双引号 -->
                  <value>QuoteFieldNames</value>
                  <!-- 是否输出值为null的字段 -->
                  <!-- <value>WriteMapNullValue</value> -->
                  <!-- 数值字段如果为null,输出为0,而非null -->
                  <value>WriteNullNumberAsZero</value>
                  <!-- List字段如果为null,输出为[],而非null -->
                  <value>WriteNullListAsEmpty</value>
                  <!-- 字符类型字段如果为null,输出为"",而非null -->
                  <value>WriteNullStringAsEmpty</value>
                  <!-- Boolean字段如果为null,输出为false,而非null -->
                  <value>WriteNullBooleanAsFalse</value>
                  <!-- null String不输出  -->
                  <value>WriteNullStringAsEmpty</value>
                  <!-- null String也要输出  -->
                  <!-- <value>WriteMapNullValue</value> -->
               </list>
            </property>
            <property name="defaultDateFormat" value="yyyy-MM-dd"></property>
         </bean>
      </mvc:message-converters>
   </mvc:annotation-driven>

   <!-- REST中根据URL后缀自动判定Content-Type及相应的View -->
   <bean  class="">
      <property name="mediaTypes" >
         <map>
            <entry key="json" value="application/json"/>
         </map>
      </property>
      <!-- 这里是否忽略掉accept header,默认就是false -->
      <property name="ignoreAcceptHeader" value="true"/>
      <property name="favorPathExtension" value="true"/>
   </bean>

   <!-- 视图文件解析配置 -->
   <bean class="">
      <property name="prefix" value="${}"/>
      <property name="suffix" value="${}"/>
   </bean>

   <!-- 对静态资源文件的访问, 将无法mapping到Controller的path交给default servlet handler处理 -->
   <mvc:default-servlet-handler/>

   <!-- 定义无Controller的path<->view直接映射 -->
   <mvc:view-controller path="/" view-name="redirect:${}"/>

   <!-- 基于注解式子的异常处理 -->
   <bean  class=""></bean>
   <!-- Shiro end -->

   <!-- 上传文件拦截,设置最大上传文件大小   10M=10*1024*1024(B)=10485760 bytes -->
   <bean  class="">
      <property name="maxUploadSize" value="${}" />
   </bean>
</beans>


第四种方案:使用SpringMVC的自定义属性编辑器

@InitBinder
protected void initBinder(WebDataBinder binder) {
    // String类型转换,将所有传递进来的String进行前后空格处理, null字符串处理
    (, new PropertyEditorSupport() {
        @Override
        public void setAsText(String text) {
            setValue(text == null ? null : ());
        }

        @Override
        public String getAsText() {
            Object value = getValue();
            return value != null ? () : "";
        }
    });

    // Date 类型转换
    (, new PropertyEditorSupport() {
        @Override
        public void setAsText(String text) {
            setValue((text));
        }

        @Override
        public String getAsText() {
            Date date = (Date) getValue();
            return (date, "yyyy-MM-dd");
        }
    });
}


DateUtils源代码:

import ;
import ;
import ;

import ;

/**
 * 日期工具类, 继承类
 * 
 */
public class DateUtils extends . {

   private static String[] parsePatterns = { "yyyy-MM-dd",
         "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy/MM/dd",
         "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm" ,"yyyyMMdd"};

   /**
    * 得到当前日期字符串 格式(yyyy-MM-dd)
    */
   public static String getDate() {
      return getDate("yyyy-MM-dd");
   }

   /**
    * 得到当前日期字符串 格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E"
    */
   public static String getDate(String pattern) {
      return (new Date(), pattern);
   }

   /**
    * 得到日期字符串 默认格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E"
    */
   public static String formatDate(Date date, Object... pattern) {
      String formatDate = null;
      if (pattern != null &&  > 0) {
         formatDate = (date, pattern[0].toString());
      } else {
         formatDate = (date, "yyyy-MM-dd");
      }
      return formatDate;
   }

   /**
    * 得到日期时间字符串,转换格式(yyyy-MM-dd HH:mm:ss)
    */
   public static String formatDateTime(Date date) {
      return formatDate(date, "yyyy-MM-dd HH:mm:ss");
   }

   /**
    * 得到当前时间字符串 格式(HH:mm:ss)
    */
   public static String getTime() {
      return formatDate(new Date(), "HH:mm:ss");
   }

   /**
    * 得到当前日期和时间字符串 格式(yyyy-MM-dd HH:mm:ss)
    */
   public static String getDateTime() {
      return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
   }

   /**
    * 得到当前年份字符串 格式(yyyy)
    */
   public static String getYear() {
      return formatDate(new Date(), "yyyy");
   }

   /**
    * 得到当前月份字符串 格式(MM)
    */
   public static String getMonth() {
      return formatDate(new Date(), "MM");
   }

   /**
    * 得到当天字符串 格式(dd)
    */
   public static String getDay() {
      return formatDate(new Date(), "dd");
   }

   /**
    * 得到当前星期字符串 格式(E)星期几
    */
   public static String getWeek() {
      return formatDate(new Date(), "E");
   }

   /**
    * 日期型字符串转化为日期 格式 { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
    * "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyyMMdd" }
    */
   public static Date parseDate(Object str) {
      if (str == null) {
         return null;
      }
      try {
         return parseDate((), parsePatterns);
      } catch (ParseException e) {
         return null;
      }
   }

   /**
    * 获取过去的天数
    * 
    * @param date
    * @return
    */
   public static long pastDays(Date date) {
      long t = new Date().getTime() - ();
      return t / (24 * 60 * 60 * 1000);
   }

   /**
    * 获取过去的小时
    * @param date
    * @return
    */
   public static long pastHour(Date date) {
      long t = new Date().getTime()-();
      return t/(60*60*1000);
   }
   
   /**
    * 获取过去的分钟
    * @param date
    * @return
    */
   public static long pastMinutes(Date date) {
      long t = new Date().getTime()-();
      return t/(60*1000);
   }
   
   /**
    * 转换为时间(天,时:分:秒.毫秒)
    * @param timeMillis
    * @return
    */
    public static String formatDateTime(long timeMillis){
      long day = timeMillis/(24*60*60*1000);
      long hour = (timeMillis/(60*60*1000)-day*24);
      long min = ((timeMillis/(60*1000))-day*24*60-hour*60);
      long s = (timeMillis/1000-day*24*60*60-hour*60*60-min*60);
      long sss = (timeMillis-day*24*60*60*1000-hour*60*60*1000-min*60*1000-s*1000);
      return (day>0?day+",":"")+hour+":"+min+":"+s+"."+sss;
    }
   
   /**
    * 获取某一天的开始时间(0点)
    * @param date
    * @return
    */
   public static Date getDateStart(Date date) {
      if (date == null) {
         return null;
      }
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      try {
         date = (formatDate(date, "yyyy-MM-dd") + " 00:00:00");
      } catch (ParseException e) {
         ();
      }
      return date;
   }

   /**
    * 获取某一天的结束时间(23:59)
    * 
    * @param date
    * @return
    */
   public static Date getDateEnd(Date date) {
      if (date == null) {
         return null;
      }
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      try {
         date = (formatDate(date, "yyyy-MM-dd") + " 23:59:59");
      } catch (ParseException e) {
         ();
      }
      return date;
   }
   
   /**
    * 比较两个日期时间的大小,反回1表示preDateStr > nextDateStr,0就相等,-1为小于
    * @author: 
    * @param preDateStr
    * @param nextDateStr
    * @return result
    */
   public static int compareDate(Object preDateStr, Object nextDateStr) {
      int result = 0;
      Date preDate = parseDate(preDateStr);
      Date nextDate = parseDate(nextDateStr);
      try {
         result =  (nextDate);
      } catch (Exception e) {
         result = 0;
         ();
      }
      return result;
   }
   
   /**
    * 获取某一天的前几天或者后几天,根据数字符号决定天数
    * @author: 
    * @param date
    * @param days
    * @return
    */
   public static String getPastDayStr(Object dateObj, int days) {
      Date date = parseDate(dateObj);
      long time = () + days * (long)(24 * 60 * 60 * 1000);
      return formatDate(new Date(time));
   }
   
   /**
    * preDateStr - nextDateStr 返回秒数
    * @author: 
    * @param preDateStr
    * @param nextDateStr
    * @return
    */
   public static long getSubactDate(Object preDateStr, Object nextDateStr) {
      Date preDate = parseDate(preDateStr);
      Date nextDate = parseDate(nextDateStr);
      long result = (() - ()) / 1000L;
      return result; 
   }
   
   /**
    * 返回过去的天数: preDateStr - nextDateStr 
    * @author: 
    * @param preDateStr
    * @param nextDateStr
    * @return
    */
   public static long getDifferDate(Object preDateStr, Object nextDateStr) {
      return getSubactDate(preDateStr, nextDateStr) / (60 * 60 * 24L);
   }
   
   /**
    * 传入日期时间与当前系统日期时间的比较,
    * 若日期相同,则根据时分秒来返回 ,
    * 否则返回具体日期
    * @author: 
    * @param updateDate 传入日期
    * @param updateTime 传入时间
    * @return 日期或者 xx小时前||xx分钟前||xx秒前
    */
   public static String getNewUpdateDateString(String updateDate, String updateTime) {
      String result = updateDate;
      long time = 0;
      if ((())) {
         time = ((), updateDate
               + " " + updateTime);
         if (time >= 3600) {
            result = time / 3600 + "小时前";
         } else if (time >= 60) {
            result = time / 60 + "分钟前";
         } else if (time >= 1) {
            result = time + "秒前";
         } else {
            result = "刚刚";
         }
      } else if (() >= 10) {
         result = (5);
      }
      return result;
   } 
}