Skip to content

Commit a5793b1

Browse files
committed
bugfix: Refactor timezone validation logic in FastDateParser to 3-digit timezone IDs, which have been deprecated in JDK 25
1 parent 9167659 commit a5793b1

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/main/java/org/apache/commons/lang3/time/FastDateParser.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.text.ParseException;
2424
import java.text.ParsePosition;
2525
import java.text.SimpleDateFormat;
26+
import java.time.ZoneId;
2627
import java.util.ArrayList;
2728
import java.util.Arrays;
2829
import java.util.Calendar;
@@ -46,7 +47,9 @@
4647

4748
import org.apache.commons.lang3.ArraySorter;
4849
import org.apache.commons.lang3.CharUtils;
50+
import org.apache.commons.lang3.JavaVersion;
4951
import org.apache.commons.lang3.LocaleUtils;
52+
import org.apache.commons.lang3.SystemUtils;
5053

5154
/**
5255
* FastDateParser is a fast and thread-safe version of {@link java.text.SimpleDateFormat}.
@@ -532,7 +535,7 @@ public String toString() {
532535
for (final String[] zoneNames : zones) {
533536
// offset 0 is the time zone ID and is not localized
534537
final String tzId = zoneNames[ID];
535-
if (tzId.equalsIgnoreCase(TimeZones.GMT_ID)) {
538+
if (isInvalidTimeZoneId(tzId)) {
536539
continue;
537540
}
538541
final TimeZone tz = TimeZone.getTimeZone(tzId);
@@ -561,7 +564,7 @@ public String toString() {
561564
}
562565
// Order is undefined.
563566
for (final String tzId : ArraySorter.sort(TimeZone.getAvailableIDs())) {
564-
if (tzId.equalsIgnoreCase(TimeZones.GMT_ID)) {
567+
if (isInvalidTimeZoneId(tzId)) {
565568
continue;
566569
}
567570
final TimeZone tz = TimeZone.getTimeZone(tzId);
@@ -612,6 +615,11 @@ public String toString() {
612615
return "TimeZoneStrategy [locale=" + locale + ", tzNames=" + tzNames + ", pattern=" + pattern + "]";
613616
}
614617

618+
private static boolean isInvalidTimeZoneId(String tzId) {
619+
return tzId.equalsIgnoreCase(TimeZones.GMT_ID)
620+
|| SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_25)
621+
&& ZoneId.SHORT_IDS.containsKey(tzId);
622+
}
615623
}
616624

617625
/**

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import java.text.DateFormatSymbols;
2525
import java.text.ParseException;
26+
import java.time.ZoneId;
2627
import java.util.ArrayList;
2728
import java.util.Comparator;
2829
import java.util.Date;
@@ -110,6 +111,9 @@ private void testTimeZoneStrategyPattern_DateFormatSymbols_getZoneStrings(final
110111

111112
final String[][] zones = getZoneStringsSorted(locale);
112113
for (final String[] zone : zones) {
114+
if (isInvalidZoneId(zone[0])) {
115+
continue;
116+
}
113117
for (int zIndex = 1; zIndex < zone.length; ++zIndex) {
114118
final String tzDisplay = zone[zIndex];
115119
if (tzDisplay == null) {
@@ -171,6 +175,9 @@ private void testTimeZoneStrategyPattern_TimeZone_getAvailableIDs(final Locale l
171175
assumeFalse(LocaleUtils.isLanguageUndetermined(locale), () -> toFailureMessage(locale, null, null));
172176
assumeTrue(LocaleUtils.isAvailableLocale(locale), () -> toFailureMessage(locale, null, null));
173177
for (final String id : ArraySorter.sort(TimeZone.getAvailableIDs())) {
178+
if (isInvalidZoneId(id)) {
179+
continue;
180+
}
174181
final TimeZone timeZone = TimeZone.getTimeZone(id);
175182
final String displayName = timeZone.getDisplayName(locale);
176183
final FastDateParser parser = new FastDateParser("z", timeZone, locale);
@@ -226,4 +233,12 @@ private String toFailureMessage(final Locale locale, final String languageTag, f
226233
return String.format("locale = %s, languageTag = '%s', isAvailableLocale = %s, isLanguageUndetermined = %s, timeZone = %s", languageTag, locale,
227234
LocaleUtils.isAvailableLocale(locale), LocaleUtils.isLanguageUndetermined(locale), TimeZones.toTimeZone(timeZone));
228235
}
236+
237+
/**
238+
* skip short IDs on Java 25+ as they are not supported there
239+
*/
240+
private boolean isInvalidZoneId(final String zoneId) {
241+
return SystemUtils.isJavaVersionAtLeast(JavaVersion.JAVA_25)
242+
&& ZoneId.SHORT_IDS.containsKey(zoneId);
243+
}
229244
}

0 commit comments

Comments
 (0)