Libathos
Libathos

Reputation: 3362

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);
dateFormat.parse(myCalendar.getTime().toString())

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

also myCalendar.getTime().toString() yields Mon Oct 31 12:27:08 GMT+03:00 2016

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

Upvotes: 0

Views: 142

Answers (2)

Kamran Ahmed
Kamran Ahmed

Reputation: 7761

You may try something like:

SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zZ yyyy", Locale.FRANCE);
dateFormat.parse(myCalendar.getTime().toString());

Upvotes: 0

Alex
Alex

Reputation: 3382

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);
Log.d("Time",dateFormat.format(myCalendar.getTime()));

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.

Upvotes: 1

Related Questions