一般狀況下,使用@requestbody json這種傳參格式的時候 日期轉換可能會遇到的問題。html
查看源碼 java
/**
* Defines a commonly used date format that conforms
* to ISO-8601 date formatting standard, when it includes basic undecorated
* timezone definition
*/
protected final static String DATE_FORMAT_STR_ISO8601 = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";spring
/**
* Same as 'regular' 8601, but handles 'Z' as an alias for "+0000"
* (or "GMT")
*/
protected final static String DATE_FORMAT_STR_ISO8601_Z = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";apache
/**
* ISO-8601 with just the Date part, no time
*/
protected final static String DATE_FORMAT_STR_PLAIN = "yyyy-MM-dd";json
/**
* This constant defines the date format specified by
* RFC 1123 / RFC 822.
*/
protected final static String DATE_FORMAT_STR_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz";
app
只支持這幾種格式 日期轉換。所以遇到了各類坑,特此分享。ide
解決方案以下:.net
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<!-- <constructor-arg>
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
</bean>
</property>
</bean>
</constructor-arg> -->
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="featuresToEnable">
<array>
<util:constant static-field="com.fasterxml.jackson.databind.SerializationFeature.WRAP_ROOT_VALUE" />
</array>
</property>
<property name="dateFormat">
<!-- 智能日期轉換 -->
<bean class="com.dateformatx.SmartDateFormat"/>
</property>
</bean>
</property>
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>orm
咱們能夠本身去定義一個日期轉換類xml
package com.dateformatx;
import java.text.FieldPosition;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Description: 智能日期轉換
* Author:
*/
public class SmartDateFormat extends SimpleDateFormat {
/**
*
*/
private static final long serialVersionUID = 6517753776479429611L;
@Override
public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition pos) {
return new StringBuffer(DateUtil.smartFormat(date));
}
@Override
public Date parse(String text) throws ParseException {
return DateUtil.smartFormat(text);
}
}
package com.dateformatx;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.lang3.StringUtils;
/**
* DateUtil類
*
* @author
*/
public class DateUtil {
public static final String Y_M_D = "yyyy-MM-dd";
public static final String Y_M_D_HM = "yyyy-MM-dd HH:mm";
public static final String Y_M_D_HMS = "yyyy-MM-dd HH:mm:ss";
public static final String YMD = "yyyyMMdd";
public static final String YMDHM = "yyyyMMddHHmm";
public static final String YMDHMS = "yyyyMMddHHmmss";
public static final String ymd = "yyyy/MM/dd";
public static final String ymd_HM = "yyyy/MM/dd HH:mm";
public static final String ymd_HMS = "yyyy/MM/dd HH:mm:ss";
public static final String Y_M = "yyyy-MM";
/**
* 智能轉換日期
*
* @param date * @return */ public static String smartFormat(Date date) { String dateStr = null; if (date == null) { dateStr = ""; } else { try { dateStr = formatDate(date, Y_M_D_HMS); //時分秒 if (dateStr.endsWith(" 00:00:00")) { dateStr = dateStr.substring(0, 10); } //時分 else if (dateStr.endsWith("00:00")) { dateStr = dateStr.substring(0, 16); } //秒 else if (dateStr.endsWith(":00")) { dateStr = dateStr.substring(0, 16); } } catch (Exception ex) { throw new IllegalArgumentException("轉換日期失敗: " + ex.getMessage(), ex); } } return dateStr; } /** * 智能轉換日期 * * @param text * @return */ public static Date smartFormat(String text) { Date date = null; try { if (text == null || text.length() == 0) { date = null; } else if (text.length() == 10) { date = formatStringToDate(text, Y_M_D); } else if (text.length() == 13) { date = new Date(Long.parseLong(text)); } else if (text.length() == 16) { date = formatStringToDate(text, Y_M_D_HM); } else if (text.length() == 19) { date = formatStringToDate(text, Y_M_D_HMS); }else if (text.length() == 7) { date = formatStringToDate(text, Y_M); }else { throw new IllegalArgumentException("日期長度不符合要求!"); } } catch (Exception e) { throw new IllegalArgumentException("日期轉換失敗!"); } return date; } /** * 獲取當前日期 * @param format * @return * @throws Exception */ public static String getNow(String format) throws Exception{ return formatDate(new Date(), format); } /** * 格式化日期格式 * * @param argDate * @param argFormat * @return 格式化後的日期字符串 */ public static String formatDate(Date argDate, String argFormat) throws Exception { if (argDate == null) { throw new Exception("參數[日期]不能爲空!"); } if (StringUtils.isEmpty(argFormat)) { argFormat = Y_M_D; } SimpleDateFormat sdfFrom = new SimpleDateFormat(argFormat); return sdfFrom.format(argDate).toString(); } /** * 把字符串格式化成日期 * * @param argDateStr * @param argFormat * @return */ public static Date formatStringToDate(String argDateStr, String argFormat) throws Exception { if (argDateStr == null || argDateStr.trim().length() < 1) { throw new Exception("參數[日期]不能爲空!"); } String strFormat = argFormat; if (StringUtils.isEmpty(strFormat)) { strFormat = Y_M_D; if (argDateStr.length() > 16) { strFormat = Y_M_D_HMS; } else if (argDateStr.length() > 10) { strFormat = Y_M_D_HM; } else if(argDateStr.length() ==7 ){ strFormat = Y_M; } } SimpleDateFormat sdfFormat = new SimpleDateFormat(strFormat); //嚴格模式 sdfFormat.setLenient(false); try { return sdfFormat.parse(argDateStr); } catch (ParseException e) { throw new Exception(e); } } }