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