Skip to content

Commit f3ba9c9

Browse files
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 <garydgregory@users.noreply.github.com>
1 parent 22d985c commit f3ba9c9

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

src/main/java/org/apache/commons/cli/Converter.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2424
import java.nio.file.Paths;
2525
import java.text.SimpleDateFormat;
2626
import java.util.Date;
27+
import java.util.Locale;
2728

2829
/**
2930
* 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> {
7677
/**
7778
* Converts a String to a {@link Date} using the format string Form "EEE MMM dd HH:mm:ss zzz yyyy".
7879
*/
79-
Converter<Date, java.text.ParseException> DATE = s -> new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(s);
80+
Converter<Date, java.text.ParseException> DATE = s -> {
81+
final String pattern = "EEE MMM dd HH:mm:ss zzz yyyy";
82+
try {
83+
return new SimpleDateFormat(pattern).parse(s);
84+
} catch (final java.text.ParseException e) {
85+
// Date.toString() always emits English month/day names, so fall back to Locale.ENGLISH
86+
// when the default locale rejects the documented format.
87+
return new SimpleDateFormat(pattern, Locale.ENGLISH).parse(s);
88+
}
89+
};
8090

8191
/**
8292
* Applies the conversion function to the String argument.

src/test/java/org/apache/commons/cli/ConverterTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2727
import java.util.ArrayList;
2828
import java.util.Date;
2929
import java.util.List;
30+
import java.util.Locale;
3031
import java.util.stream.Stream;
3132

3233
import org.junit.jupiter.api.Test;
@@ -93,6 +94,16 @@ void testDateLocaleDe() throws Exception {
9394
assertEquals(expected, Converter.DATE.apply(formatted));
9495
}
9596

97+
@Test
98+
@DefaultLocale(language = "de", country = "DE")
99+
void testDateLocaleDeEnglishInput() throws Exception {
100+
// Date.toString() always emits English month/day names, so the converter must still parse
101+
// them when the default locale is not English.
102+
final Date expected = new Date(1023400137000L);
103+
final String formatted = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH).format(expected);
104+
assertEquals(expected, Converter.DATE.apply(formatted));
105+
}
106+
96107
@Test
97108
void testFile() throws Exception {
98109
final URL url = this.getClass().getClassLoader().getResource("./org/apache/commons/cli/existing-readable.file");

0 commit comments

Comments
 (0)