From 88803df41bac36bbbd5f633db690a1f0b7fef601 Mon Sep 17 00:00:00 2001 From: Naveed Khan Date: Sun, 28 Jun 2026 20:19:48 +0530 Subject: [PATCH 1/2] parse Converter.DATE with default locale, fall back to Locale.ENGLISH --- src/main/java/org/apache/commons/cli/Converter.java | 11 ++++++++++- .../java/org/apache/commons/cli/ConverterTests.java | 11 +++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/apache/commons/cli/Converter.java b/src/main/java/org/apache/commons/cli/Converter.java index 811a73bb2..7699fda55 100644 --- a/src/main/java/org/apache/commons/cli/Converter.java +++ b/src/main/java/org/apache/commons/cli/Converter.java @@ -24,6 +24,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.nio.file.Paths; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.Locale; /** * The definition of the functional interface to call when doing a conversion. Like {@code Function} but can throw an Exception. @@ -76,7 +77,15 @@ public interface Converter { /** * Converts a String to a {@link Date} using the format string Form "EEE MMM dd HH:mm:ss zzz yyyy". */ - Converter DATE = s -> new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(s); + Converter DATE = s -> { + try { + return new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(s); + } catch (final java.text.ParseException e) { + // Date.toString() always emits English month/day names, so fall back to Locale.ENGLISH + // when the default locale rejects the documented format. + return new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH).parse(s); + } + }; /** * Applies the conversion function to the String argument. diff --git a/src/test/java/org/apache/commons/cli/ConverterTests.java b/src/test/java/org/apache/commons/cli/ConverterTests.java index adfec60f5..a92a13558 100644 --- a/src/test/java/org/apache/commons/cli/ConverterTests.java +++ b/src/test/java/org/apache/commons/cli/ConverterTests.java @@ -27,6 +27,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.Locale; import java.util.stream.Stream; import org.junit.jupiter.api.Test; @@ -93,6 +94,16 @@ void testDateLocaleDe() throws Exception { assertEquals(expected, Converter.DATE.apply(formatted)); } + @Test + @DefaultLocale(language = "de", country = "DE") + void testDateLocaleDeEnglishInput() throws Exception { + // Date.toString() always emits English month/day names, so the converter must still parse + // them when the default locale is not English. + final Date expected = new Date(1023400137000L); + final String formatted = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH).format(expected); + assertEquals(expected, Converter.DATE.apply(formatted)); + } + @Test void testFile() throws Exception { final URL url = this.getClass().getClassLoader().getResource("./org/apache/commons/cli/existing-readable.file"); From 92f0c5a76e1cd3a2ecc5c13bea58089614191165 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Sun, 28 Jun 2026 11:06:15 -0400 Subject: [PATCH 2/2] No copy-pasta. --- src/main/java/org/apache/commons/cli/Converter.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/apache/commons/cli/Converter.java b/src/main/java/org/apache/commons/cli/Converter.java index 7699fda55..bc1212838 100644 --- a/src/main/java/org/apache/commons/cli/Converter.java +++ b/src/main/java/org/apache/commons/cli/Converter.java @@ -78,12 +78,13 @@ public interface Converter { * Converts a String to a {@link Date} using the format string Form "EEE MMM dd HH:mm:ss zzz yyyy". */ Converter DATE = s -> { + final String pattern = "EEE MMM dd HH:mm:ss zzz yyyy"; try { - return new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(s); + return new SimpleDateFormat(pattern).parse(s); } catch (final java.text.ParseException e) { // Date.toString() always emits English month/day names, so fall back to Locale.ENGLISH // when the default locale rejects the documented format. - return new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH).parse(s); + return new SimpleDateFormat(pattern, Locale.ENGLISH).parse(s); } };