DateFormatter.java 5.8 KB
Newer Older
1
/*
2
 * Copyright 2002-2013 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
16

17
package org.springframework.format.datetime;
18 19 20 21

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
22
import java.util.Collections;
23
import java.util.Date;
24
import java.util.HashMap;
25
import java.util.Locale;
26
import java.util.Map;
27
import java.util.TimeZone;
28

29
import org.springframework.format.Formatter;
30 31 32
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;
import org.springframework.util.StringUtils;
33 34

/**
K
polish  
Keith Donald 已提交
35
 * A formatter for {@link java.util.Date} types.
36
 * Allows the configuration of an explicit date pattern and locale.
37
 *
38
 * @author Keith Donald
39
 * @author Juergen Hoeller
40
 * @author Phillip Webb
41
 * @since 3.0
42
 * @see SimpleDateFormat
43
 */
44
public class DateFormatter implements Formatter<Date> {
45

46
	private static final Map<ISO, String> ISO_PATTERNS;
J
Juergen Hoeller 已提交
47

48
	static {
49
		Map<ISO, String> formats = new HashMap<DateTimeFormat.ISO, String>(4);
50 51 52 53 54 55 56
		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);
	}


57 58 59 60
	private String pattern;

	private int style = DateFormat.DEFAULT;

61 62 63 64
	private String stylePattern;

	private ISO iso;

65 66 67 68
	private TimeZone timeZone;

	private boolean lenient = false;

69 70

	/**
71
	 * Create a new default DateFormatter.
72
	 */
73 74 75 76 77 78 79 80 81
	public DateFormatter() {
	}

	/**
	 * Create a new DateFormatter for the given date pattern.
	 */
	public DateFormatter(String pattern) {
		this.pattern = pattern;
	}
82 83 84


	/**
85 86
	 * Set the pattern to use to format date values.
	 * <p>If not specified, DateFormat's default style will be used.
87 88 89 90 91
	 */
	public void setPattern(String pattern) {
		this.pattern = pattern;
	}

92 93 94 95 96 97 98 99 100
	/**
	 * Set the ISO format used for this date.
	 * @param iso the {@link ISO} format
	 * @since 3.2
	 */
	public void setIso(ISO iso) {
		this.iso = iso;
	}

101 102 103 104 105 106 107 108 109 110 111 112 113
	/**
	 * Set the style to use to format date values.
	 * <p>If not specified, DateFormat's default style will be used.
	 * @see DateFormat#DEFAULT
	 * @see DateFormat#SHORT
	 * @see DateFormat#MEDIUM
	 * @see DateFormat#LONG
	 * @see DateFormat#FULL
	 */
	public void setStyle(int style) {
		this.style = style;
	}

114 115 116 117 118 119 120 121 122 123
	/**
	 * 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
	 * <ul>
	 * <li>'S' = Small</li>
	 * <li>'M' = Medium</li>
	 * <li>'L' = Long</li>
	 * <li>'F' = Full</li>
	 * <li>'-' = Omitted</li>
	 * <ul>
124
	 * This method mimics the styles supported by Joda-Time.
125 126 127 128 129 130 131
	 * @param stylePattern two characters from the set {"S", "M", "L", "F", "-"}
	 * @since 3.2
	 */
	public void setStylePattern(String stylePattern) {
		this.stylePattern = stylePattern;
	}

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
	/**
	 * Set the TimeZone to normalize the date values into, if any.
	 */
	public void setTimeZone(TimeZone timeZone) {
		this.timeZone = timeZone;
	}

	/**
	 * Specify whether or not parsing is to be lenient. Default is false.
	 * <p>With lenient parsing, the parser may allow inputs that do not precisely match the format.
	 * With strict parsing, inputs must match the format exactly.
	 */
	public void setLenient(boolean lenient) {
		this.lenient = lenient;
	}

148

149
	@Override
150
	public String print(Date date, Locale locale) {
151 152 153
		return getDateFormat(locale).format(date);
	}

154
	@Override
155 156
	public Date parse(String text, Locale locale) throws ParseException {
		return getDateFormat(locale).parse(text);
157 158 159
	}


160
	protected DateFormat getDateFormat(Locale locale) {
161
		DateFormat dateFormat = createDateFormat(locale);
162 163 164 165
		if (this.timeZone != null) {
			dateFormat.setTimeZone(this.timeZone);
		}
		dateFormat.setLenient(this.lenient);
166
		return dateFormat;
167 168
	}

169 170 171 172
	private DateFormat createDateFormat(Locale locale) {
		if (StringUtils.hasLength(this.pattern)) {
			return new SimpleDateFormat(this.pattern, locale);
		}
J
Juergen Hoeller 已提交
173 174 175 176 177
		if (this.iso != null && this.iso != ISO.NONE) {
			String pattern = ISO_PATTERNS.get(this.iso);
			if (pattern == null) {
				throw new IllegalStateException("Unsupported ISO format " + this.iso);
			}
178 179 180 181
			SimpleDateFormat format = new SimpleDateFormat(pattern);
			format.setTimeZone(TimeZone.getTimeZone("UTC"));
			return format;
		}
J
Juergen Hoeller 已提交
182
		if (StringUtils.hasLength(this.stylePattern)) {
183 184
			int dateStyle = getStylePatternForChar(0);
			int timeStyle = getStylePatternForChar(1);
J
Juergen Hoeller 已提交
185
			if (dateStyle != -1 && timeStyle != -1) {
186 187
				return DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale);
			}
J
Juergen Hoeller 已提交
188
			if (dateStyle != -1) {
189 190
				return DateFormat.getDateInstance(dateStyle, locale);
			}
J
Juergen Hoeller 已提交
191
			if (timeStyle != -1) {
192 193
				return DateFormat.getTimeInstance(timeStyle, locale);
			}
J
Juergen Hoeller 已提交
194
			throw new IllegalStateException("Unsupported style pattern '"+ this.stylePattern+ "'");
195 196 197 198 199 200

		}
		return DateFormat.getDateInstance(this.style, locale);
	}

	private int getStylePatternForChar(int index) {
J
Juergen Hoeller 已提交
201 202
		if (this.stylePattern != null && this.stylePattern.length() > index) {
			switch (this.stylePattern.charAt(index)) {
203 204 205 206 207 208 209
				case 'S': return DateFormat.SHORT;
				case 'M': return DateFormat.MEDIUM;
				case 'L': return DateFormat.LONG;
				case 'F': return DateFormat.FULL;
				case '-': return -1;
			}
		}
J
Juergen Hoeller 已提交
210
		throw new IllegalStateException("Unsupported style pattern '" + this.stylePattern + "'");
211
	}
212

213
}