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 0add789ed fix negative week year formatting in FastDatePrinter (#1762)
0add789ed is described below

commit 0add789ed39fb3d59ce16048a3960821ee313bf7
Author: alhuda <[email protected]>
AuthorDate: Thu Jul 30 16:25:48 2026 +0530

    fix negative week year formatting in FastDatePrinter (#1762)
    
    GregorianCalendar.getWeekYear() is proleptic, so a BC date yields a 
negative week year that WeekYear passed straight to the digit rules. Those 
rules build characters arithmetically, so the value walked below '0' and 
emitted control characters instead of digits. Emit the sign in WeekYear and 
pass the magnitude down, matching TimeZoneNumberRule.
---
 .../apache/commons/lang3/time/FastDatePrinter.java |  9 +++++++--
 .../commons/lang3/time/FastDatePrinterTest.java    | 22 ++++++++++++++++++++++
 2 files changed, 29 insertions(+), 2 deletions(-)

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 fa30f30f1..3b6f7b6fd 100644
--- a/src/main/java/org/apache/commons/lang3/time/FastDatePrinter.java
+++ b/src/main/java/org/apache/commons/lang3/time/FastDatePrinter.java
@@ -864,11 +864,16 @@ private static final class WeekYear implements NumberRule 
{
         public void appendTo(final Appendable buffer, final Calendar calendar) 
throws IOException {
             // 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));
+            appendTo(buffer, calendar.isWeekDateSupported() ? 
calendar.getWeekYear() : calendar.get(Calendar.YEAR));
         }
 
         @Override
-        public void appendTo(final Appendable buffer, final int value) throws 
IOException {
+        public void appendTo(final Appendable buffer, int value) throws 
IOException {
+            // A week year is proleptic, so a BC date gives a negative value 
the digit rules cannot render.
+            if (value < 0) {
+                buffer.append('-');
+                value = -value;
+            }
             rule.appendTo(buffer, value);
         }
 
diff --git 
a/src/test/java/org/apache/commons/lang3/time/FastDatePrinterTest.java 
b/src/test/java/org/apache/commons/lang3/time/FastDatePrinterTest.java
index 83f525388..2d1d9cdaa 100644
--- a/src/test/java/org/apache/commons/lang3/time/FastDatePrinterTest.java
+++ b/src/test/java/org/apache/commons/lang3/time/FastDatePrinterTest.java
@@ -446,4 +446,26 @@ void testWeekYear() {
         assertEquals("2021", printer4DigitAnotherFallback.format(cal));
         assertEquals("21", printer2Digits.format(cal));
     }
+
+    @DefaultLocale(language = "en", country = "US")
+    @DefaultTimeZone("America/New_York")
+    @Test
+    void testWeekYearBc() {
+        final GregorianCalendar cal = new GregorianCalendar(42, Calendar.JULY, 
15);
+        cal.set(Calendar.ERA, GregorianCalendar.BC);
+        assertEquals(-41, cal.getWeekYear());
+        assertEquals(new SimpleDateFormat("YYYY").format(cal.getTime()), 
getInstance("YYYY").format(cal));
+        assertEquals(new SimpleDateFormat("YYYYY").format(cal.getTime()), 
getInstance("YYYYY").format(cal));
+        assertEquals(new SimpleDateFormat("YY").format(cal.getTime()), 
getInstance("YY").format(cal));
+        assertEquals("-0041", getInstance("YYYY").format(cal));
+        assertEquals("-00041", getInstance("YYYYY").format(cal));
+        assertEquals("-41", getInstance("YY").format(cal));
+        // Padded to four digits like the AD case, see testWeekYear.
+        assertEquals("-0041", getInstance("YYY").format(cal));
+        assertEquals("-0041", getInstance("Y").format(cal));
+        // The week year of 1 BC is 0, which the digit rules already render.
+        final GregorianCalendar oneBc = new GregorianCalendar(1, 
Calendar.JULY, 15);
+        oneBc.set(Calendar.ERA, GregorianCalendar.BC);
+        assertEquals("0000", getInstance("YYYY").format(oneBc));
+    }
 }

Reply via email to