|
23 | 23 |
|
24 | 24 | import java.time.Instant; |
25 | 25 | import java.time.ZoneId; |
| 26 | +import java.time.ZoneOffset; |
26 | 27 | import java.time.format.DateTimeParseException; |
27 | 28 | import java.time.temporal.TemporalAccessor; |
28 | 29 | import java.util.Locale; |
@@ -130,23 +131,37 @@ public void testLocales() { |
130 | 131 | assertThat(DateFormatters.forPattern("strict_date_optional_time").getLocale(), is(Locale.ROOT)); |
131 | 132 | Locale locale = randomLocale(random()); |
132 | 133 | assertThat(DateFormatters.forPattern("strict_date_optional_time").withLocale(locale).getLocale(), is(locale)); |
133 | | - IllegalArgumentException e = |
134 | | - expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_millis").withLocale(locale)); |
135 | | - assertThat(e.getMessage(), is("epoch_millis date formatter can only be in locale ROOT")); |
136 | | - e = expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_second").withLocale(locale)); |
137 | | - assertThat(e.getMessage(), is("epoch_second date formatter can only be in locale ROOT")); |
| 134 | + if (locale.equals(Locale.ROOT)) { |
| 135 | + DateFormatter millisFormatter = DateFormatters.forPattern("epoch_millis"); |
| 136 | + assertThat(millisFormatter.withLocale(locale), is(millisFormatter)); |
| 137 | + DateFormatter secondFormatter = DateFormatters.forPattern("epoch_second"); |
| 138 | + assertThat(secondFormatter.withLocale(locale), is(secondFormatter)); |
| 139 | + } else { |
| 140 | + IllegalArgumentException e = |
| 141 | + expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_millis").withLocale(locale)); |
| 142 | + assertThat(e.getMessage(), is("epoch_millis date formatter can only be in locale ROOT")); |
| 143 | + e = expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_second").withLocale(locale)); |
| 144 | + assertThat(e.getMessage(), is("epoch_second date formatter can only be in locale ROOT")); |
| 145 | + } |
138 | 146 | } |
139 | 147 |
|
140 | 148 | public void testTimeZones() { |
141 | 149 | // zone is null by default due to different behaviours between java8 and above |
142 | 150 | assertThat(DateFormatters.forPattern("strict_date_optional_time").getZone(), is(nullValue())); |
143 | 151 | ZoneId zoneId = randomZone(); |
144 | 152 | assertThat(DateFormatters.forPattern("strict_date_optional_time").withZone(zoneId).getZone(), is(zoneId)); |
145 | | - IllegalArgumentException e = |
146 | | - expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_millis").withZone(zoneId)); |
147 | | - assertThat(e.getMessage(), is("epoch_millis date formatter can only be in zone offset UTC")); |
148 | | - e = expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_second").withZone(zoneId)); |
149 | | - assertThat(e.getMessage(), is("epoch_second date formatter can only be in zone offset UTC")); |
| 153 | + if (zoneId.equals(ZoneOffset.UTC)) { |
| 154 | + DateFormatter millisFormatter = DateFormatters.forPattern("epoch_millis"); |
| 155 | + assertThat(millisFormatter.withZone(zoneId), is(millisFormatter)); |
| 156 | + DateFormatter secondFormatter = DateFormatters.forPattern("epoch_second"); |
| 157 | + assertThat(secondFormatter.withZone(zoneId), is(secondFormatter)); |
| 158 | + } else { |
| 159 | + IllegalArgumentException e = |
| 160 | + expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_millis").withZone(zoneId)); |
| 161 | + assertThat(e.getMessage(), is("epoch_millis date formatter can only be in zone offset UTC")); |
| 162 | + e = expectThrows(IllegalArgumentException.class, () -> DateFormatters.forPattern("epoch_second").withZone(zoneId)); |
| 163 | + assertThat(e.getMessage(), is("epoch_second date formatter can only be in zone offset UTC")); |
| 164 | + } |
150 | 165 | } |
151 | 166 |
|
152 | 167 | public void testEqualsAndHashcode() { |
|
0 commit comments