Java – unparseable Greek date – simpledateformat
I'm trying to use simpledateformat to read a string representing a Greek date and time (such as "28") Μαρτίου 2014,14:00 "), but it will throw Java text. ParseException:Unparseable date:“28 Μαρτίου 2014,14:00 "error
This is an example 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 also tried locales el and el_ GR, but no luck
Any suggestions?
Solution
a) First of all, never use the expression new locale ("El GR"), but new locale ("El", "GR") or new locale ("El") without country / region. Please refer to Javadoc to use the constructor correctly (because there is no language code "El GR")
b) The exceptions you observe (and me, but not everyone) are caused by different localized resources of the underlying JVM My JVM Certificate (1.6.0_31):
Locale locale = new Locale("el"); DateFormatSymbols dfs = DateFormatSymbols.getInstance(locale); for (String m : dfs.getMonths()) { System.out.println(m); } // output Μάρτιος Απρίλιος Μάϊος Ιούνιος Ιούλιος Αύγουστος Σεπτέμβριος Οκτώβριος Νοέμβριος Δεκέμβριος
Descriptions of different data can be found in cldr - repository for localization resources Modern Greek knows at least two different forms of March( Μαρτίου Vs independent form Μάρτιος). Java version 6 uses a stand - alone form, while java version 7 uses a common form
See also this compatibility note for Java version 8, where you can choose to specify the format mode (independent or not):
So the obvious solution is to update to Java 7 The external library is not helpful here because no one has their own Greek resources today However, if you are forced to continue using java 6 for any reason, following a clumsy solution will help:
Locale locale = new Locale("el","GR"); SimpleDateFormat formatter = new SimpleDateFormat("dd MMMM yyyy,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(); }