android - DateFormat.getDateTimeInstance 在 android 5 中使用错误的本地时间格式

标签 android localization

我使用以下代码来格式化我的输出:

    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/

相关文章:

java - Java 中是否有 C# 的 HttpServerUtility.UrlTokenEncode 的等效项?

iPhone 本地化 - 获取手机的语言代码?

c++ - 在用非托管 C 编写的应用程序中实现本地化的最佳解决方案/架构是什么?

iOS 本地化图像未显示

Android Instrumentation 启动 Activity

android - 授权完成,出现错误 com.twitter.sdk.android.core.TwitterAuthException : Authorize failed

android - 是否可以对多种语言使用相同的字符串资源文件?

flash - 多语言闪存

android - 使用 Retrofit 检查响应的数据类型

android - 是否可以对XML中的属性值执行算术运算