須要maven, spring 版本 4.2.5.RELEASEjava
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.jaxrs</groupId> <artifactId>jackson-jaxrs-base</artifactId> <version>2.7.3</version> </dependency>
mvc 數據綁定, 從字符串到Datespring
import java.text.ParseException; import java.util.Collection; import java.util.Date; import java.util.HashSet; import java.util.Set; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.time.DateUtils; import org.springframework.core.convert.converter.Converter; public final class StringToDate implements Converter<String, Date>{ private final Set<String> patterns; public StringToDate(){ patterns = new HashSet<>(); patterns.add("yyyy-MM-dd HH:mm:ss"); } @Override public Date convert(String source) { if(StringUtils.isBlank(source)) return null; Date parseDate = null; try { parseDate = DateUtils.parseDate(source, patterns.toArray(new String[]{})); } catch (ParseException e) { throw new IllegalArgumentException(e.getCause()); } return parseDate; } /** * 自定義轉換 默認有 * {@code yyyy-MM-dd HH:mm:ss} * @param patterns */ public void setPatterns(Collection<String> patterns) { this.patterns.addAll(patterns); } }
spring mvc 配置文件apache
<mvc:annotation-driven conversion-service="conversionService"> <mvc:message-converters> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="objectMapper"> <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean" p:indentOutput="true" p:simpleDateFormat="yyyy-MM-dd HH:mm:ss"> </bean> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- 類型轉換 --> <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="com.xx.support.StringToDate"> <property name="patterns"> <set> <value><![CDATA[yyyy/MM/dd HH:mm:ss]]></value> </set> </property> </bean> </set> </property> </bean>
附:額外未完成工做json
想使用jackson json to Object, 日期也支持多種格式匹配mvc
import java.io.IOException; import java.text.ParseException; import java.util.Date; import org.apache.commons.lang3.time.DateUtils; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; public class JsonDateDeserialize extends JsonDeserializer<Date> { private String[] patterns = {"yyyy-MM-dd HH:mm:ss","yyyy/MM/dd HH:m:ss"}; public Date deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { String dateAsString = jp.getText(); Date parseDate = null; try { parseDate = DateUtils.parseDate(dateAsString, patterns); } catch (ParseException e) { throw new IllegalArgumentException(e.getCause()); } return parseDate; } }
發現須要得到 ObjectMapper
app
ObjectMapper mapper = new ObjectMapper(); SimpleModule module = new SimpleModule(); module.addDeserializer(Date.class, new JsonDateDeserialize ()); mapper.registerModule(module);