Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

java - Joda-Time Invalid format exception for Hebrew language

I want to try parse so simple date on Java-11.

Date language is: Hebrew Date format is: "MMM YYYY dd" Example date is: "18 ??? 2010"

And my code looks like:

DateTimeFormatter f = DateTimeFormat.forPattern("MMM YYYY dd")
                                    .withLocale(Locale.forLanguageTag("iw-IL"));
String dateInString = "18 ??? 2010";
Date d = f.parseDateTime(dateInString).toDate();
System.out.println(d);

But when I try it, I get an error like:

Exception in thread "main" java.lang.IllegalArgumentException: Invalid format: "18 ??? 2010" is malformed at "??? 2010"
    at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:945)

Is there any suggestion?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Reading direction

Your format pattern string , MMM YYYY dd, says that the month abbreviation comes first, then year, then day of month. It’s impossible to tell from the look, but when I copy-paste your date string, 18 ??? 2010, then day of month (18) comes first, then the month abbreviation (??? for October) and then finally the year (2010). Since Hebrew is read right to left and the rest left to right, the string looks the same in both cases. And since the order really does not agree behind the scenes, parsing fails.

So the solutions are two (at least):

  1. Fix your date string to match the format pattern.

  2. Fix your format pattern to match the date string:

    DateTimeFormatter f = DateTimeFormat.forPattern("dd MMM YYYY")
            .withLocale(Locale.forLanguageTag("iw-IL"));
    

In the latter case output from your code is (in my time zone):

Mon Oct 18 00:00:00 CEST 2010

It doesn’t work readily on all Java versions

… but this doesnt work on java-11. But it works on java-8

That’s because the default locale data have changed in Java 9. Until Java 8 Java’s own locale data were used as the default. From Java 9 CLDR (Unicode Common Locale Date Repository) is the default. Here the Hebrew abbreviation for October is ????. For how to make parsing of the string you have got work on Java 9, 11 and later, see JDK dateformatter parsing DayOfWeek in German locale, java8 vs java9.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...