|
4 | 4 | import org.joda.time.DateTimeZone; |
5 | 5 | import org.joda.time.format.DateTimeFormat; |
6 | 6 | import org.joda.time.format.DateTimeFormatter; |
| 7 | +import org.joda.time.format.DateTimeFormatterBuilder; |
| 8 | +import org.joda.time.format.DateTimeParser; |
7 | 9 |
|
8 | 10 | import java.util.Locale; |
9 | 11 |
|
10 | 12 | public class FormattedDateTime |
11 | 13 | { |
12 | 14 | private final DateTimeFormatter formatter; |
13 | 15 |
|
14 | | - public FormattedDateTime(DateTimeFormatter formatter) |
| 16 | + public FormattedDateTime(DateTimeFormatter formatter, DateTimeFormatter... formatters) |
15 | 17 | { |
16 | | - this.formatter = formatter; |
| 18 | + this.formatter = new DateTimeFormatterBuilder().append(formatter.getPrinter(), parsers(formatter, formatters)).toFormatter(); |
17 | 19 | } |
18 | 20 |
|
19 | | - public FormattedDateTime(String pattern) |
| 21 | + public FormattedDateTime(String pattern, String... patterns) |
20 | 22 | { |
21 | | - this(DateTimeFormat.forPattern(pattern)); |
| 23 | + this.formatter = new DateTimeFormatterBuilder().append(DateTimeFormat.forPattern(pattern).getPrinter(), parsers(pattern, patterns)).toFormatter(); |
22 | 24 | } |
23 | 25 |
|
24 | 26 | public FormattedDateTime() |
25 | 27 | { |
26 | 28 | this("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); |
27 | 29 | } |
28 | 30 |
|
| 31 | + private DateTimeParser[] parsers(DateTimeFormatter formatter, DateTimeFormatter... formatters) |
| 32 | + { |
| 33 | + DateTimeParser[] parsers = new DateTimeParser[formatters.length + 1]; |
| 34 | + parsers[0] = formatter.getParser(); |
| 35 | + |
| 36 | + for (int i = 0; i < formatters.length; i++) |
| 37 | + { |
| 38 | + parsers[i + 1] = formatters[i].getParser(); |
| 39 | + } |
| 40 | + |
| 41 | + return parsers; |
| 42 | + } |
| 43 | + |
| 44 | + private DateTimeParser[] parsers(String pattern, String... patterns) |
| 45 | + { |
| 46 | + DateTimeParser[] parsers = new DateTimeParser[patterns.length + 1]; |
| 47 | + parsers[0] = DateTimeFormat.forPattern(pattern).getParser(); |
| 48 | + |
| 49 | + for (int i = 0; i < patterns.length; i++) |
| 50 | + { |
| 51 | + parsers[i + 1] = DateTimeFormat.forPattern(patterns[i]).getParser(); |
| 52 | + } |
| 53 | + |
| 54 | + return parsers; |
| 55 | + } |
| 56 | + |
29 | 57 | // ============================================================================================ |
30 | 58 |
|
31 | 59 | public String date(DateTime dateTime, DateTimeZone timeZone, Locale locale, String defaultValue) |
|
0 commit comments