2

I trying to parse following 11 Ноя 1980 г. string into date. This string is in russian formatting. I am using this code snippet to parse this.

Locale locale = new Locale("ru", "RU");
System.out.println(locale.toString());
DateFormat full = DateFormat.getDateInstance(DateFormat.LONG, locale);
try {
    Date date = full.parse("11 Ноя 1980 г.");
    System.out.println(date);
} catch (ParseException e) {
    e.printStackTrace();
}

While running this code as an normal java program outputs in parsed date object, but running this on android gives following exception.

11-20 03:52:45.545 11241-11241/com.marcow.birthdaylist W/System.err: java.text.ParseException: Unparseable date: "11 Ноя 1980 г." (at offset 3)
11-20 03:52:45.546 11241-11241/com.marcow.birthdaylist W/System.err:     at java.text.DateFormat.parse(DateFormat.java:579)
11-20 03:52:45.546 11241-11241/com.marcow.birthdaylist W/System.err:     at com.marcow.birthdaylist.TestActivity.onCreate(TestActivity.java:44)
11-20 03:52:45.546 11241-11241/com.marcow.birthdaylist W/System.err:     at android.app.Activity.performCreate(Activity.java:6237)
11-20 03:52:45.546 11241-11241/com.marcow.birthdaylist W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
11-20 03:52:45.546 11241-11241/com.marcow.birthdaylist W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
11-20 03:52:45.546 11241-11241/com.marcow.birthdaylist W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
11-20 03:52:45.546 11241-11241/com.marcow.birthdaylist W/System.err:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
11-20 03:52:45.546 11241-11241/com.marcow.birthdaylist W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
11-20 03:52:45.546 11241-11241/com.marcow.birthdaylist W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
11-20 03:52:45.546 11241-11241/com.marcow.birthdaylist W/System.err:     at android.os.Looper.loop(Looper.java:148)
11-20 03:52:45.546 11241-11241/com.marcow.birthdaylist W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
11-20 03:52:45.546 11241-11241/com.marcow.birthdaylist W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
11-20 03:52:45.546 11241-11241/com.marcow.birthdaylist W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
11-20 03:52:45.546 11241-11241/com.marcow.birthdaylist W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

I don't understand why it's behaving differently on java and android.

kimkevin
  • 2,202
  • 1
  • 26
  • 48
Vishvendra Singh
  • 195
  • 2
  • 13

2 Answers2

3

Seems like short names of the months in Russian locale encodes in different ways in JDK and Adnroid SDK. This should help you:

    String testDate = "11 Ноя 1980 г.";
    String[] months = {"Янв", "Фев", "Мар", "Апр", "Мая", "Июн", "Июл", "Авг", "Сен", "Окт", "Ноя", "Дек"};
    Locale ru = new Locale("ru");
    DateFormatSymbols symbols = DateFormatSymbols.getInstance(ru);
    symbols.setMonths(months);
    SimpleDateFormat format = new SimpleDateFormat("dd MMM yyyy 'г.'", ru);
    format.setDateFormatSymbols(symbols);
    try {
        Log.d(TAG, "Date is: " + format.parse(testDate));
    } catch (Exception e) {
        Log.e(TAG, "Error while parsing", e);
    }

I highly recommend to test it on various versions of Android before using in production!

Nikolay
  • 1,429
  • 1
  • 13
  • 24
1

You need to change this line of code:

DateFormat full = DateFormat.getDateInstance(DateFormat.LONG, locale);

to

 SimpleDateFormat full= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", locale);

Note: Here in format you can describe whatever format you want

Or try this link: Parsing string to date with timezone in national format

Sara Tirmizi
  • 417
  • 4
  • 10