|
31 | 31 | import java.time.Instant; |
32 | 32 | import java.time.LocalDate; |
33 | 33 | import java.time.ZoneId; |
34 | | -import java.time.ZoneOffset; |
35 | 34 | import java.time.format.DateTimeFormatter; |
36 | 35 | import java.util.Calendar; |
37 | 36 | import java.util.Date; |
@@ -282,6 +281,23 @@ void testLang1641() { |
282 | 281 | FastDateFormat.getInstance(ISO_8601_DATE_FORMAT, TimeZone.getTimeZone("Australia/Yancowinna"))); |
283 | 282 | } |
284 | 283 |
|
| 284 | + /** |
| 285 | + * See LANG-1791 https://issues.apache.org/jira/browse/LANG-1791. |
| 286 | + */ |
| 287 | + @Test |
| 288 | + @DefaultTimeZone("America/Toronto") |
| 289 | + void testLang1791() { |
| 290 | + final Instant now = Instant.now(); |
| 291 | + final String pattern = "yyyyMMddHH"; |
| 292 | + final FastDateFormat gmtFormatter = FastDateFormat.getInstance(pattern, TimeZones.GMT); |
| 293 | + final Calendar gmtCal = Calendar.getInstance(TimeZones.GMT); |
| 294 | + final String gmtString = gmtFormatter.format(gmtCal); |
| 295 | + assertEquals(DateTimeFormatter.ofPattern(pattern).withZone(ZoneId.of("GMT")).format(now), gmtString); |
| 296 | + final FastDateFormat defaultFormatter = FastDateFormat.getInstance(pattern); |
| 297 | + final String defaultString = defaultFormatter.format(gmtCal); |
| 298 | + assertEquals(DateTimeFormatter.ofPattern(pattern).withZone(ZoneId.systemDefault()).format(now), defaultString); |
| 299 | + } |
| 300 | + |
285 | 301 | /** |
286 | 302 | * According to LANG-954 (https://issues.apache.org/jira/browse/LANG-954) this is broken in Android 2.1. |
287 | 303 | */ |
@@ -388,27 +404,4 @@ void testTimeDefaults() { |
388 | 404 | assertEquals(FastDateFormat.getTimeInstance(FastDateFormat.LONG), |
389 | 405 | FastDateFormat.getTimeInstance(FastDateFormat.LONG, TimeZone.getDefault(), Locale.getDefault())); |
390 | 406 | } |
391 | | - |
392 | | - /* |
393 | | - * org.apache.commons.lang3.time.FastDateFormat |
394 | | - * In dateFormatter.format(timeCal), when time was not set explicitly, would make e.g. |
395 | | - * the following timestamps in Toronto at 10 am on 2025-09-17: |
396 | | - * "2025091714" - GMT, as expected, when using commons-lang3 v3.0.1 |
397 | | - * "2025091710" - EDT, when using commons-lang3 v3.18.0 or 3.19.0 (likely some other versions <3.18.0 too, but I did not test that) |
398 | | - */ |
399 | | - @Test |
400 | | - public void fastDateFormatFormatterUsingCalendarShouldMakeGmtTimestamp() { |
401 | | - final Instant now = Instant.now(); |
402 | | - final ZoneId zoneId = ZoneId.systemDefault(); // e.g. America/Toronto |
403 | | - final ZoneOffset offset = zoneId.getRules().getOffset(now); |
404 | | - System.out.printf("Current time: %s, zone: %s, offset: %sh\n", now, zoneId, offset); |
405 | | - // some legacy code that should still work the same after commons-lang3 minor ver. upgrade but it does not: |
406 | | - final FastDateFormat dateFormatter = FastDateFormat.getInstance("yyyyMMddHH"); |
407 | | - final TimeZone timeZone = TimeZone.getTimeZone("GMT"); |
408 | | - final Calendar timeCal = Calendar.getInstance(timeZone); |
409 | | - final String timestamp = dateFormatter.format(timeCal); // makes local zone timestamp when commons-lang3 v3.18.0, 3.19.0 is used (ignores Calendar's zone). |
410 | | - // expected GMT timestamp |
411 | | - final String expected = DateTimeFormatter.ofPattern("yyyyMMddHH").withZone(ZoneId.of("GMT")).format(now); |
412 | | - assertEquals(expected, timestamp); |
413 | | - } |
414 | 407 | } |
0 commit comments