Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unparseable Greek date - SimpleDateFormat

I am trying to read a string that represents a Greek date-time (like "28 Μαρτίου 2014, 14:00") using SimpleDateFormat, but it throws a java.text.ParseException: Unparseable date: "28 Μαρτίου 2014, 14:00" error.

Here is a sample code:

Locale locale = new Locale("el-GR");
SimpleDateFormat formatter = new SimpleDateFormat("dd MMMM yyyy, HH:mm", locale);
try {
     sDate = (Date) formatter.parse("28 Μαρτίου 2014, 14:00");
 } catch (ParseException ex) {
     ex.printStackTrace();
 }

I've also tried locales el and el_GR but no luck.

Any suggestions?

like image 892
Omen Avatar asked Jan 14 '15 09:01

Omen


2 Answers

a) First to say, never user the expression new Locale("el-GR"), instead use new Locale("el", "GR") or without country new Locale("el"), see javadoc for correct usage of constructors (because there is no language code "el-GR").

b) The exception you observe (and me, too, but not everyone) is caused by the different localization resources of the underlying JVM. Proof on my JVM (1.6.0_31):

Locale locale = new Locale("el");
DateFormatSymbols dfs = DateFormatSymbols.getInstance(locale);
for (String m : dfs.getMonths()) {
    System.out.println(m);
}

// output
Μάρτιος
Απρίλιος
Μάϊος
Ιούνιος
Ιούλιος
Αύγουστος
Σεπτέμβριος
Οκτώβριος
Νοέμβριος
Δεκέμβριος

The explanation of different data can be found in CLDR-repository for localized resources. Modern greek knows at least two different forms for the month March (Μαρτίου vs the standalone form Μάρτιος). Java-version 6 uses the standalone form while Java-version 7 uses the normal form.

See also this compatibility note for java-version 8 where you have options to specify the format mode (standalone or not):

When formatting date-time values using DateFormat and SimpleDateFormat, context sensitive month names are supported for languages that have the formatting and standalone forms of month names. For example, the preferred month name for January in the Czech language is ledna in the formatting form, while it is leden in the standalone form. The getMonthNames and getShortMonthNames methods of DateFormatSymbols return month names in the formatting form for those languages. Note that the month names returned by DateFormatSymbols were in the standalone form until Java SE 7. You can specify the formatting and/or standalone forms with the Calendar.getDisplayName and Calendar.getDisplayNames methods...

So the obvious solution would be updating to Java 7. External libraries will not help here because today there is no one which has its own resources for Greek. However, if you are forced for any reason to continue with Java 6 then following awkward workaround will help:

Locale locale = new Locale("el", "GR");
SimpleDateFormat formatter = new SimpleDateFormat("dd MMMM yyyy, HH:mm", locale);
DateFormatSymbols dfs = DateFormatSymbols.getInstance(locale);
String[] months = {"Ιανουαρίου", "Φεβρουαρίου", "Μαρτίου", "Απριλίου", "Μαΐου", "Ιουνίου", "Ιουλίου", "Αυγούστου", "Σεπτεμβρίου", "Οκτωβρίου", "Νοεμβρίου", "Δεκεμβρίου"};
dfs.setMonths(months);
formatter.setDateFormatSymbols(dfs);

try {
     System.out.println(formatter.parse("28 Μαρτίου 2014, 14:00"));
     // output in my timezone: Fri Mar 28 14:00:00 CET 2014
} catch (ParseException ex) {
     ex.printStackTrace();
}
like image 200
Meno Hochschild Avatar answered Oct 12 '22 14:10

Meno Hochschild


The following piece of code works for me. Double check the characters used. You problem was also a misspelled character

public static void main(String [] args) {
    Locale locale = new Locale("el", "GR");
    SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy, HH:mm", locale);
    try {
        System.out.println(formatter.parse("28 Ιανουάριος 2014, 14:00"));
        System.out.println(formatter.parse("28 Φεβρουάριος 2014, 14:00"));
        System.out.println(formatter.parse("28 Μάρτιος 2014, 14:00"));
        System.out.println(formatter.parse("28 Απρίλιος 2014, 14:00"));
        System.out.println(formatter.parse("28 Μάϊος 2014, 14:00"));
        System.out.println(formatter.parse("28 Ιούνιος 2014, 14:00"));
        System.out.println(formatter.parse("28 Ιούλιος 2014, 14:00"));
        System.out.println(formatter.parse("28 Αύγουστος 2014, 14:00"));
        System.out.println(formatter.parse("28 Σεπτέμβριος 2014, 14:00"));
        System.out.println(formatter.parse("28 Οκτώβριος 2014, 14:00"));
        System.out.println(formatter.parse("28 Νοέμβριος 2014, 14:00"));
        System.out.println(formatter.parse("28 Δεκέμβριος 2014, 14:00"));

     } catch (ParseException ex) {
         ex.printStackTrace();
     }
}

Output :

Tue Jan 28 14:00:00 IST 2014
Fri Feb 28 14:00:00 IST 2014
Fri Mar 28 14:00:00 IST 2014
Mon Apr 28 14:00:00 IST 2014
Wed May 28 14:00:00 IST 2014
Sat Jun 28 14:00:00 IST 2014
Mon Jul 28 14:00:00 IST 2014
Thu Aug 28 14:00:00 IST 2014
Sun Sep 28 14:00:00 IST 2014
Tue Oct 28 14:00:00 IST 2014
Fri Nov 28 14:00:00 IST 2014
Sun Dec 28 14:00:00 IST 2014
like image 20
shikjohari Avatar answered Oct 12 '22 13:10

shikjohari