我使用以下代码来格式化我的输出:
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT);
Date date = new Date(cursor.getLong(cursor.getColumnIndex(MyContentProvider.KEY_TIME_START)));
TextView textView = (TextView) view.findViewById(R.id.column_time_start);
textView.setText(df.format(date));
例如德语 我希望采用本地 24 小时时间格式。
使用 Android 4(例如 4.4.2),时间格式符合预期。
但对于 Android 5(例如 5.1),时间采用 12 小时时间格式。
有人知道解决这个问题的好方法吗?
最佳答案
尝试使用DateFormat.getDateTimeInstance(int, int, Locale)
:
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, Locale.getDefault());
String formattedDate = dateFormat .format(new Date());
如果这对您不起作用,请尝试以下操作:
Locale current = getResources().getConfiguration().locale;
// Find out what the current locale is and format your date based on that
关于android - DateFormat.getDateTimeInstance 在 android 5 中使用错误的本地时间格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31629406/