diff --git a/build.gradle b/build.gradle index 6d97c7be17..aa9b657402 100644 --- a/build.gradle +++ b/build.gradle @@ -262,12 +262,12 @@ project('spring-context') { } compile("org.beanshell:bsh:2.0b4", optional) compile("org.codehaus.groovy:groovy-all:1.6.3", optional) - compile("org.jruby:jruby:1.4.0", optional) compile("org.hibernate:hibernate-validator:4.2.0.Final") { dep -> optional dep exclude group: 'org.slf4j', module: 'slf4j-api' } compile("joda-time:joda-time:1.6", optional) + compile("org.jruby:jruby:1.4.0", optional) compile("javax.cache:cache-api:0.5", optional) compile("net.sf.ehcache:ehcache-core:2.0.0", optional) compile("org.slf4j:slf4j-api:1.6.1", optional) diff --git a/spring-context/src/main/java/org/springframework/format/annotation/DateTimeFormat.java b/spring-context/src/main/java/org/springframework/format/annotation/DateTimeFormat.java index 8646fa3415..c5e526e8e0 100644 --- a/spring-context/src/main/java/org/springframework/format/annotation/DateTimeFormat.java +++ b/spring-context/src/main/java/org/springframework/format/annotation/DateTimeFormat.java @@ -26,7 +26,7 @@ import java.lang.annotation.Target; * Supports formatting by style pattern, ISO date time pattern, or custom format pattern string. * Can be applied to java.util.Date, java.util.Calendar, java.long.Long, or Joda Time fields. *

- * For style-based formatting, set the {@link #style()} attribute to be the style pattern code. + * For style-based formatting, set the {@link #style()} attribute to be the style pattern code. * The first character of the code is the date style, and the second character is the time style. * Specify a character of 'S' for short style, 'M' for medium, 'L' for long, and 'F' for full. * A date or time may be omitted by specifying the style character '-'. @@ -39,7 +39,7 @@ import java.lang.annotation.Target; * When the pattern attribute is specified, it takes precedence over both the style and ISO attribute. * When the iso attribute is specified, if takes precedence over the style attribute. * When no annotation attributes are specified, the default format applied is style-based with a style code of 'SS' (short date, short time). - * + * * @author Keith Donald * @author Juergen Hoeller * @since 3.0 @@ -76,23 +76,23 @@ public @interface DateTimeFormat { * Common ISO date time format patterns. */ public enum ISO { - - /** + + /** * The most common ISO Date Format yyyy-MM-dd e.g. 2000-10-31. */ DATE, - /** - * The most common ISO Time Format hh:mm:ss.SSSZ e.g. 01:30:00.000-05:00. + /** + * The most common ISO Time Format HH:mm:ss.SSSZ e.g. 01:30:00.000-05:00. */ TIME, - /** - * The most common ISO DateTime Format yyyy-MM-dd'T'hh:mm:ss.SSSZ e.g. 2000-10-31 01:30:00.000-05:00. + /** + * The most common ISO DateTime Format yyyy-MM-dd'T'HH:mm:ss.SSSZ e.g. 2000-10-31 01:30:00.000-05:00. * The default if no annotation value is specified. */ DATE_TIME, - + /** * Indicates that no ISO-based format pattern should be applied. */ diff --git a/spring-context/src/main/java/org/springframework/format/datetime/DateFormatter.java b/spring-context/src/main/java/org/springframework/format/datetime/DateFormatter.java index a371b4cf90..365800aba8 100644 --- a/spring-context/src/main/java/org/springframework/format/datetime/DateFormatter.java +++ b/spring-context/src/main/java/org/springframework/format/datetime/DateFormatter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,11 +19,18 @@ package org.springframework.format.datetime; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.Collections; import java.util.Date; +import java.util.HashMap; import java.util.Locale; +import java.util.Map; import java.util.TimeZone; import org.springframework.format.Formatter; +import org.springframework.format.annotation.DateTimeFormat; +import org.springframework.format.annotation.DateTimeFormat.ISO; +import org.springframework.util.Assert; +import org.springframework.util.StringUtils; /** * A formatter for {@link java.util.Date} types. @@ -31,15 +38,30 @@ import org.springframework.format.Formatter; * * @author Keith Donald * @author Juergen Hoeller + * @author Phillip Webb * @since 3.0 - * @see SimpleDateFormat + * @see SimpleDateFormat */ public class DateFormatter implements Formatter { + private static final Map ISO_PATTERNS; + static { + Map formats = new HashMap(); + formats.put(ISO.DATE, "yyyy-MM-dd"); + formats.put(ISO.TIME, "HH:mm:ss.SSSZ"); + formats.put(ISO.DATE_TIME, "yyyy-MM-dd'T'HH:mm:ss.SSSZ"); + ISO_PATTERNS = Collections.unmodifiableMap(formats); + } + + private String pattern; private int style = DateFormat.DEFAULT; + private String stylePattern; + + private ISO iso; + private TimeZone timeZone; private boolean lenient = false; @@ -80,6 +102,32 @@ public class DateFormatter implements Formatter { this.style = style; } + /** + * Set the two character to use to format date values. The first character used for + * the date style, the second is for the time style. Supported characters are + *