Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/main/java/org/apache/commons/cli/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String,T>} but can throw an Exception.
Expand Down Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/org/apache/commons/cli/ConverterTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
Loading