俄语语言环境中的日期解析问题
Issue with date parsing in russian locale
我试图将以下 11 Ноя 1980 г.
字符串解析为日期。此字符串采用俄语格式。我正在使用此代码片段来解析它。
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();
}
虽然 运行 此代码作为正常 java 程序在解析的日期对象中输出,但 运行 android 上的此代码给出以下异常。
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)
我不明白为什么它在 java 和 android 上表现不同。
您需要更改这行代码:
DateFormat full = DateFormat.getDateInstance(DateFormat.LONG, locale);
to
SimpleDateFormat full= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", locale);
注意:这里的格式你可以描述任何你想要的格式
Or try this link: Parsing string to date with timezone in national format
似乎俄语语言环境中月份的简称在 JDK 和 Adnroid SDK 中以不同的方式编码。
这应该可以帮助您:
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);
}
我强烈建议在生产中使用之前在 Android 的各种版本上对其进行测试!
我试图将以下 11 Ноя 1980 г.
字符串解析为日期。此字符串采用俄语格式。我正在使用此代码片段来解析它。
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();
}
虽然 运行 此代码作为正常 java 程序在解析的日期对象中输出,但 运行 android 上的此代码给出以下异常。
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)
我不明白为什么它在 java 和 android 上表现不同。
您需要更改这行代码:
DateFormat full = DateFormat.getDateInstance(DateFormat.LONG, locale);
to
SimpleDateFormat full= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", locale);
注意:这里的格式你可以描述任何你想要的格式
Or try this link: Parsing string to date with timezone in national format
似乎俄语语言环境中月份的简称在 JDK 和 Adnroid SDK 中以不同的方式编码。 这应该可以帮助您:
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);
}
我强烈建议在生产中使用之前在 Android 的各种版本上对其进行测试!