View Javadoc
1   /*
2    * Copyright 2002-2015 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.format.annotation;
18  
19  import java.lang.annotation.Documented;
20  import java.lang.annotation.ElementType;
21  import java.lang.annotation.Retention;
22  import java.lang.annotation.RetentionPolicy;
23  import java.lang.annotation.Target;
24  
25  /**
26   * Declares that a field should be formatted as a date time.
27   *
28   * <p>Supports formatting by style pattern, ISO date time pattern, or custom format pattern string.
29   * Can be applied to {@code java.util.Date}, {@code java.util.Calendar}, {@code java.long.Long},
30   * Joda-Time value types; and as of Spring 4 and JDK 8, to JSR-310 <code>java.time</code> types too.
31   *
32   * <p>For style-based formatting, set the {@link #style} attribute to be the style pattern code.
33   * The first character of the code is the date style, and the second character is the time style.
34   * Specify a character of 'S' for short style, 'M' for medium, 'L' for long, and 'F' for full.
35   * A date or time may be omitted by specifying the style character '-'.
36   *
37   * <p>For ISO-based formatting, set the {@link #iso} attribute to be the desired {@link ISO} format,
38   * such as {@link ISO#DATE}. For custom formatting, set the {@link #pattern} attribute to be the
39   * DateTime pattern, such as {@code yyyy/MM/dd hh:mm:ss a}.
40   *
41   * <p>Each attribute is mutually exclusive, so only set one attribute per annotation instance
42   * (the most convenient one for your formatting needs).
43   * When the pattern attribute is specified, it takes precedence over both the style and ISO attribute.
44   * When the iso attribute is specified, it takes precedence over the style attribute.
45   * When no annotation attributes are specified, the default format applied is style-based
46   * with a style code of 'SS' (short date, short time).
47   *
48   * @author Keith Donald
49   * @author Juergen Hoeller
50   * @since 3.0
51   * @see org.joda.time.format.DateTimeFormat
52   */
53  @Documented
54  @Retention(RetentionPolicy.RUNTIME)
55  @Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.ANNOTATION_TYPE})
56  public @interface DateTimeFormat {
57  
58  	/**
59  	 * The style pattern to use to format the field.
60  	 * <p>Defaults to 'SS' for short date time. Set this attribute when you wish to format
61  	 * your field in accordance with a common style other than the default style.
62  	 */
63  	String style() default "SS";
64  
65  	/**
66  	 * The ISO pattern to use to format the field.
67  	 * The possible ISO patterns are defined in the {@link ISO} enum.
68  	 * <p>Defaults to {@link ISO#NONE}, indicating this attribute should be ignored.
69  	 * Set this attribute when you wish to format your field in accordance with an ISO format.
70  	 */
71  	ISO iso() default ISO.NONE;
72  
73  	/**
74  	 * The custom pattern to use to format the field.
75  	 * <p>Defaults to empty String, indicating no custom pattern String has been specified.
76  	 * Set this attribute when you wish to format your field in accordance with a custom
77  	 * date time pattern not represented by a style or ISO format.
78  	 */
79  	String pattern() default "";
80  
81  
82  	/**
83  	 * Common ISO date time format patterns.
84  	 */
85  	public enum ISO {
86  
87  		/**
88  		 * The most common ISO Date Format {@code yyyy-MM-dd},
89  		 * e.g. 2000-10-31.
90  		 */
91  		DATE,
92  
93  		/**
94  		 * The most common ISO Time Format {@code HH:mm:ss.SSSZ},
95  		 * e.g. 01:30:00.000-05:00.
96  		 */
97  		TIME,
98  
99  		/**
100 		 * The most common ISO DateTime Format {@code yyyy-MM-dd'T'HH:mm:ss.SSSZ},
101 		 * e.g. 2000-10-31 01:30:00.000-05:00.
102 		 * <p>This is the default if no annotation value is specified.
103 		 */
104 		DATE_TIME,
105 
106 		/**
107 		 * Indicates that no ISO-based format pattern should be applied.
108 		 */
109 		NONE
110 	}
111 
112 }