libathos libathos - 4 months ago 28
Android Question

Parsing a date into a different locale doesn't work

I am trying to parse a date into a different locale. so instead of Monday 31 October it would be Lundi 31 Octobre (for French). I'm using the DateFormat class as suggested by the official Android documentation. So basically this is the code snippet I'm using:

dateFormat = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);

But It's throwing a Parsing Exception when I'm executing it.

Mon Oct 31 12:27:08 GMT+03:00 2016

Which I think is correct I cannot understand why dateFormat cannot parse it correctly.


So your problem is that you are trying to parse a date but you give the wrong format for the formatter. Instead of converting the calendar date back to string and then parse it you could use the dateFormat.format method.

Calendar myCalendar = Calendar.getInstance();
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL, Locale.FRANCE);

The output would be

jeudi 3 novembre 2016

You can play with the format changing the style or using SimpleDateFormat and define the output format which suits your needs.