我尝试将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上表现不同。
答案 0 :(得分:3)
似乎俄语语言环境中的短名称在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上进行测试!
答案 1 :(得分:1)
您需要更改以下代码行:
DateFormat full = DateFormat.getDateInstance(DateFormat.LONG, locale);
到
SimpleDateFormat full= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", locale);
注意:您可以在格式中描述您想要的任何格式
或尝试此链接:Parsing string to date with timezone in national format