Skip to content

Commit a22cfcd

Browse files
committed
1 parent 21e65f8 commit a22cfcd

File tree

1 file changed

+17
-24
lines changed

1 file changed

+17
-24
lines changed

src/test/java/org/apache/commons/lang3/time/FastDateFormatTest.java

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import java.time.Instant;
3232
import java.time.LocalDate;
3333
import java.time.ZoneId;
34-
import java.time.ZoneOffset;
3534
import java.time.format.DateTimeFormatter;
3635
import java.util.Calendar;
3736
import java.util.Date;
@@ -282,6 +281,23 @@ void testLang1641() {
282281
FastDateFormat.getInstance(ISO_8601_DATE_FORMAT, TimeZone.getTimeZone("Australia/Yancowinna")));
283282
}
284283

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+
285301
/**
286302
* According to LANG-954 (https://issues.apache.org/jira/browse/LANG-954) this is broken in Android 2.1.
287303
*/
@@ -388,27 +404,4 @@ void testTimeDefaults() {
388404
assertEquals(FastDateFormat.getTimeInstance(FastDateFormat.LONG),
389405
FastDateFormat.getTimeInstance(FastDateFormat.LONG, TimeZone.getDefault(), Locale.getDefault()));
390406
}
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-
}
414407
}

0 commit comments

Comments
 (0)