4

I'm trying to format a date time object with a german locale but i got an error.

String dateString = "Mi Mai 09 09:17:24 2018";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EE MMM dd HH:mm:ss yyyy", Locale.GERMAN);
LocalDateTime dateTime = LocalDateTime.parse(dateString, dtf);

The error:

java.time.format.DateTimeParseException: Text 'Mi Mai 09 09:17:24 2018' could not be parsed at index 0
10
  • According to java2s.com/Tutorials/Java/Java_Date_Time/… - the day of week has four different formats - E, EE, EEE, and EEEE. EE, as you used, is a numeric format, parsing Mi would probably require usnig EEE. Commented May 9, 2018 at 7:58
  • 3
    Btw, it works just fine with jdk1.8.0_162 but fails with jdk-10.jdk Commented May 9, 2018 at 7:59
  • @OndraK. i'm trying it with EEE and it doesn't solve the problem Commented May 9, 2018 at 8:02
  • 1
    Checked the code on my IDE and it works just fine. I used the following imports and openjdk8_144: 1. import java.time.LocalDateTime; 2. import java.time.format.DateTimeFormatter; Commented May 9, 2018 at 8:03
  • And also it fails with 8u112 (ideone.com/Fxcd0w), though I did not test it locally Commented May 9, 2018 at 8:06

2 Answers 2

6

Solution here and now:

System.setProperty("java.locale.providers", "COMPAT,CLDR");
String dateString = "Mi Mai 09 09:17:24 2018";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EE MMM dd HH:mm:ss yyyy", Locale.GERMAN);
LocalDateTime dateTime = LocalDateTime.parse(dateString, dtf);
System.out.println(dateTime);

Output (tested on Java 9 and Java 10):

2018-05-09T09:17:24

The only change from your code is I have inserted System.setProperty("java.locale.providers", "COMPAT,CLDR"); at the beginning of the program. According to the documentation this shouldn’t really work, but it did when I tried it. Instead the correct way is to supply the same system property on the command line when running your Java program. For example:

java -Djava.locale.providers=COMPAT,CLDR com.ajax.ParseTwoLetterDayOfWeekAbbreviationInGerman

Thanks to Joep Weijers for pointing out in a comment that the property must be set on the command line. Funnily the same system property broke the code on my Java 8.

More modern solution:

String dateString = "Mi. Mai 09 09:17:24 2018";

I have required a dot (a period) after Mi in the string to signify that it is an abbreviation.

Locale providers

To parse a date in German Java needs so-called locale data, including the names and abbreviations for days of the week and for months used in German and other languages. To confuse things, these data come from more than one source. In all of Java 8, 9 and 10 Java contains locale data from at least two sources, Java’s own locale data and standardized locale data from CLDR, Unicode Common Locale Data Repository (I don’t know if there is one or two more sources). In Java 8 Java’s own data were the default and you would need to specify CLDR if you wanted those data instead. In Java 9 and later it’s the other way around: CLDR is the default, but the old Java data are available as COMPAT. This explains the comment by soon that your code works on Java 8, but fails on Java 10.

And apparently the Java locale data have Mi without a dot as abbreviation for Mittwoch (Wednesday), while CLDR has Mi. with a dot. There are many other differences.

Links

Sign up to request clarification or add additional context in comments.

1 Comment

You must set the property on the command line. According to the Javadocs: "The search order of locale sensitive services can be configured by using the "java.locale.providers" system property. This system property declares the user's preferred order for looking up the locale sensitive services separated by a comma. It is only read at the Java runtime startup, so the later call to System.setProperty() won't affect the order."
-4
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;



/**
 *
 * @author Ali Hassan
 */
public class LeapTEST {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        String dateString = "Mi Mai 09 09:17:24 2018";
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EE MMM d HH:mm:ss yyyy", Locale.GERMAN);
    LocalDateTime dateTime = LocalDateTime.parse(dateString, dtf);
    System.out.print(dateTime);
    }

}

5 Comments

How this answers the question?
This is not my question. However your answer contains the same code as in the question without any explanations
ArrayList<String> mylist = new ArrayList<>(); String dateString = "Mi Mai 09 09:17:24 2018"; DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EE MMM d HH:mm:ss yyyy", Locale.GERMAN); LocalDateTime dateTime = LocalDateTime.parse(dateString, dtf); mylist.add(0, dateString); mylist.add(1, dateTime.toString()); System.out.print(mylist.get(0));
ideone.com/w3ugne - it does not work. Please read comments to the question
on Ide it work fine but on the java 9 ,10 i'm n't sure this work or not

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.