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-cli.git
The following commit(s) were added to refs/heads/master by this push:
new f3ba9c95 Use Locale.ENGLISH when parsing dates in Converter.DATE (#426)
f3ba9c95 is described below
commit f3ba9c9518edf3702a8d06d211a91959611ff413
Author: Dexter.k <[email protected]>
AuthorDate: Sun Jun 28 15:08:19 2026 +0000
Use Locale.ENGLISH when parsing dates in Converter.DATE (#426)
* parse Converter.DATE with default locale, fall back to Locale.ENGLISH
* No copy-pasta.
---------
Co-authored-by: Gary Gregory <[email protected]>
---
src/main/java/org/apache/commons/cli/Converter.java | 12 +++++++++++-
src/test/java/org/apache/commons/cli/ConverterTests.java | 11 +++++++++++
2 files changed, 22 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 811a73bb..bc121283 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 @@ import java.nio.file.Path;
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<String,T>} but can throw an Exception.
@@ -76,7 +77,16 @@ public interface Converter<T, E extends Exception> {
/**
* Converts a String to a {@link Date} using the format string Form "EEE
MMM dd HH:mm:ss zzz yyyy".
*/
- Converter<Date, java.text.ParseException> DATE = s -> new
SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(s);
+ Converter<Date, java.text.ParseException> DATE = s -> {
+ final String pattern = "EEE MMM dd HH:mm:ss zzz yyyy";
+ try {
+ 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(pattern, 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 adfec60f..a92a1355 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 @@ import java.text.SimpleDateFormat;
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 @@ public class ConverterTests {
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");