Mybatis 使用@requestbody 日期自定义转换 http://my.oschina.net/u/1992570/blog/715475
通常情况下,使用@requestbody json这种传参格式的时候 日期转换可能会遇到的问题。
查看源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
/** * 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"; /** * 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'"; /** * ISO-8601 with just the Date part, no time */ protected final static String DATE_FORMAT_STR_PLAIN = "yyyy-MM-dd"; /** * 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"; |
只支持这几种格式 日期转换。因此遇到了各种坑,特此分享。
解决方案如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<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> |
我们可以自己去定义一个日期转换类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
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); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
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); } } } |