This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push:
new 68c333308 FastDatePrinter WeekYear rule calls Calendar.getWeekYear()
without checking isWeekDateSupported() (#1659)
68c333308 is described below
commit 68c33330820f94f1f335e16eeb201988ad8c1edd
Author: Gary Gregory <[email protected]>
AuthorDate: Mon May 18 12:37:00 2026 -0400
FastDatePrinter WeekYear rule calls Calendar.getWeekYear() without checking
isWeekDateSupported() (#1659)
The method would throw UnsupportedOperationException.
---
.../apache/commons/lang3/time/FastDatePrinter.java | 4 ++-
.../commons/lang3/time/FastDateFormatTest.java | 30 ++++++++++++++++++++++
2 files changed, 33 insertions(+), 1 deletion(-)
diff --git a/src/main/java/org/apache/commons/lang3/time/FastDatePrinter.java
b/src/main/java/org/apache/commons/lang3/time/FastDatePrinter.java
index 6063e8aaf..42a309f02 100644
--- a/src/main/java/org/apache/commons/lang3/time/FastDatePrinter.java
+++ b/src/main/java/org/apache/commons/lang3/time/FastDatePrinter.java
@@ -862,7 +862,9 @@ private static final class WeekYear implements NumberRule {
@Override
public void appendTo(final Appendable buffer, final Calendar calendar)
throws IOException {
- rule.appendTo(buffer, calendar.getWeekYear());
+ // Some Calendar implementations (JapaneseImperialCalendar) do not
support week-dates.
+ // Fall back to Calendar.YEAR in that case.
+ rule.appendTo(buffer, calendar.isWeekDateSupported() ?
calendar.getWeekYear() : calendar.get(Calendar.YEAR));
}
@Override
diff --git
a/src/test/java/org/apache/commons/lang3/time/FastDateFormatTest.java
b/src/test/java/org/apache/commons/lang3/time/FastDateFormatTest.java
index b992164e8..6c084b634 100644
--- a/src/test/java/org/apache/commons/lang3/time/FastDateFormatTest.java
+++ b/src/test/java/org/apache/commons/lang3/time/FastDateFormatTest.java
@@ -110,6 +110,36 @@ private AtomicLongArray measureTime(final Format printer,
final Format parser) t
return totalElapsed;
}
+ /**
+ * Pre-patch: UnsupportedOperationException when formatting a Japanese
Imperial Calendar with 'Y' (week-year) pattern.
+ * <p>
+ * Post-patch: falls back to Calendar.YEAR and formats successfully.
+ * </p>
+ */
+ @Test
+ void testJapaneseImperialCalendarWeekYearDoesNotThrow() {
+ final Locale japaneseImperial = new Locale("ja", "JP", "JP");
+ final Calendar japaneseCal = Calendar.getInstance(japaneseImperial);
+ final FastDateFormat fdp = FastDateFormat.getInstance("YYYY-MM-dd");
+ assertNotNull(fdp.format(japaneseCal), "Formatting
JapaneseImperialCalendar with YYYY pattern must not throw
UnsupportedOperationException");
+ }
+
+ /**
+ * Pre-patch: UnsupportedOperationException when formatting a Japanese
Imperial
+ * <p>
+ * Calendar with 'Y' (week-year) pattern. Post-patch: falls back to
Calendar.YEAR and formats successfully.
+ * </p>
+ * <p>
+ * Also test that a regular Gregorian calendar works fine with YYYY.
+ * </p>
+ */
+ @Test
+ void testGregorianCalendarWeekYearWorks() {
+ final Calendar cal = Calendar.getInstance();
+ final FastDateFormat fdp = FastDateFormat.getInstance("YYYY-MM-dd");
+ assertNotNull(fdp.format(cal), "Formatting Gregorian Calendar with
YYYY pattern should always work");
+ }
+
@DefaultLocale(language = "en", country = "US")
@Test
void test_changeDefault_Locale_DateInstance() {