Unsurpassed Greek Date - SimpleDateFormat

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

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 also tried the el and el_GR locales, but no luck.

Any suggestions?

+6
source share
4 answers

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

b) The exception you are observing (and me too, but not all) is caused by various 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 Μάρτιος Απρίλιος Μάϊος Ιούνιος Ιούλιος Αύγουστος Σεπτέμβριος Οκτώβριος Νοέμβριος Δεκέμβριος 

An explanation of the various data can be found in the CLDR repository for localized resources. Modern Greek knows at least two different forms of the month of March (Μαρτίου versus the autonomous form Μάρτιος). Java version 6 uses a standalone form, while Java version 7 uses a regular form.

See also the compatibility note for java version 8, where you have options for specifying the format mode (standalone or not):

When formatting date and time values ​​using DateFormat and SimpleDateFormat, contextual monthly names are supported for languages ​​that have formatting and stand-alone forms of the month names. For example, the preferred month name for January in Czech is in the form of formatting, while the stand-alone form. The getMonthNames and getShortMonthNames methods DateFormatSymbols returns the names of the months in a formatting form for those languages. Note that the month names returned by DateFormatSymbols were offline before Java SE 7 . You can specify formatting and / or stand-alone forms using Calendar.getDisplayName and Calendar.getDisplayNames methods ...

Thus, the obvious solution will be upgraded to Java 7 . External libraries will not help here, because today there is no one who has their own resources for the Greek language. However, if for some reason you are forced to continue working with Java 6, then an inconvenient 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(); } 
+11
source

The following code snippet works for me. Double check the characters used. Your problem was also an error symbol

 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 
+1
source

This work for me:

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

You can get more information on this blog.

https://grooovygeorge.wordpress.com/2012/03/15/why-simpledateformatter-isnt-really-simple-with-locales/

0
source
 String target = "28 Μαρτίου 2014, 14:00"; Locale locale = new Locale("el", "GR"); try { DateFormat df = new SimpleDateFormat("dd MMM yyyy, HH:mm", locale); Date result = df.parse(target); System.out.println(result); } catch(ParseException e) { e.printStackTrace(); } 
-1
source

Source: https://habr.com/ru/post/980976/


All Articles