Polishing

This commit is contained in:
Juergen Hoeller
2013-10-04 22:53:49 +02:00
parent 8b3afda6f4
commit 52cca48f40
6 changed files with 50 additions and 48 deletions

View File

@@ -29,7 +29,6 @@ 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;
/**
@@ -45,6 +44,7 @@ import org.springframework.util.StringUtils;
public class DateFormatter implements Formatter<Date> {
private static final Map<ISO, String> ISO_PATTERNS;
static {
Map<ISO, String> formats = new HashMap<DateTimeFormat.ISO, String>(4);
formats.put(ISO.DATE, "yyyy-MM-dd");
@@ -170,34 +170,36 @@ public class DateFormatter implements Formatter<Date> {
if (StringUtils.hasLength(this.pattern)) {
return new SimpleDateFormat(this.pattern, locale);
}
if (iso != null && iso != ISO.NONE) {
String pattern = ISO_PATTERNS.get(iso);
Assert.state(pattern != null, "Unsupported ISO format " + iso);
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);
}
SimpleDateFormat format = new SimpleDateFormat(pattern);
format.setTimeZone(TimeZone.getTimeZone("UTC"));
return format;
}
if(StringUtils.hasLength(stylePattern)) {
if (StringUtils.hasLength(this.stylePattern)) {
int dateStyle = getStylePatternForChar(0);
int timeStyle = getStylePatternForChar(1);
if(dateStyle != -1 && timeStyle != -1) {
if (dateStyle != -1 && timeStyle != -1) {
return DateFormat.getDateTimeInstance(dateStyle, timeStyle, locale);
}
if(dateStyle != -1) {
if (dateStyle != -1) {
return DateFormat.getDateInstance(dateStyle, locale);
}
if(timeStyle != -1) {
if (timeStyle != -1) {
return DateFormat.getTimeInstance(timeStyle, locale);
}
throw new IllegalStateException("Unsupported style pattern '"+ stylePattern+ "'");
throw new IllegalStateException("Unsupported style pattern '"+ this.stylePattern+ "'");
}
return DateFormat.getDateInstance(this.style, locale);
}
private int getStylePatternForChar(int index) {
if(stylePattern != null && stylePattern.length() > index) {
switch (stylePattern.charAt(index)) {
if (this.stylePattern != null && this.stylePattern.length() > index) {
switch (this.stylePattern.charAt(index)) {
case 'S': return DateFormat.SHORT;
case 'M': return DateFormat.MEDIUM;
case 'L': return DateFormat.LONG;
@@ -205,7 +207,7 @@ public class DateFormatter implements Formatter<Date> {
case '-': return -1;
}
}
throw new IllegalStateException("Unsupported style pattern '"+ stylePattern+ "'");
throw new IllegalStateException("Unsupported style pattern '" + this.stylePattern + "'");
}
}