diff --git a/src/main/java/org/apache/commons/cli/Converter.java b/src/main/java/org/apache/commons/cli/Converter.java index 811a73bb2..bc1212838 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,16 @@ 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 -> { + 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 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");