6

我是一名法国 Android 开发人员,因此使用Locale.getDefault()会导致我DateFormat使用 24 小时模式。但是,当我通过设置菜单手动将我的设备设置为 12 小时模式时,DateFormat会继续以 24 小时格式运行。

相反,TimePickers是根据我自己的12/24小时设置来设置的。

有没有办法让DateFormats 的行为与TimePickers 相同?

编辑:

这是我的DateFormat声明:

timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault());

在这里,我将我的设置TimePicker为 12 或 24 小时模式。

tp.setIs24HourView(android.text.format.DateFormat.is24HourFormat((Context) this));

我的解决方案:

根据@Meno Hochschild 下面的回答,这是我解决这个棘手问题的方法:

boolean is24hour = android.text.format.DateFormat.is24HourFormat((Context) this);
tp.setIs24HourView(is24hour); // tp is the TimePicker
timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault());
if (timeFormat instanceof SimpleDateFormat) {
    String pattern = ((SimpleDateFormat) timeFormat).toPattern();
    if (is24hour) {
        timeFormat = new SimpleDateFormat(pattern.replace("h", "H").replace(" a",""), Locale.getDefault());
    }
    else {
        timeFormat = new SimpleDateFormat(pattern.replace("H", "h"), Locale.getDefault());
    }
}

在此之后,timeFormat无论您的设备设置为以 24 小时格式还是 12 小时格式显示时间,都将正确格式化日期。并且TimePicker也将正确设置。

4

4 回答 4

6

如果您已指定模式,SimpleDateFormat则您已修复 12/24 小时模式,模式符号“h”(1-12)的 12 小时模式或模式符号“的 24 小时模式” H" (0-23)。备选方案“k”和“K”相似,但范围略有不同。

也就是说,指定模式使您的格式独立于设备设置!

另一种方法是使用DateFormat.getDateTimeInstance(),它使时间样式依赖于系统语言环境(如果Locale.getDefault()可能发生变化 - 或者您必须部署一种机制如何询问当前设备语言环境,然后在 Android-Java 中进行设置Locale.setDefault())。

Android 特有的另一个想法是使用字符串常量TIME_12_24直接询问系统设置,然后指定依赖于该设置的模式。这似乎也可以通过特殊方法DateFormat.is24HourFormat()实现(请注意,Android 有两个不同的类 name DateFormat)。这种方法的具体例子:

boolean twentyFourHourStyle = 
  android.text.format.DateFormat.is24HourFormat((Context) this);
DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault());
if (df instanceof SimpleDateFormat) {
  String pattern = ((SimpleDateFormat) df).toPattern();
  if (twentyFourHourStyle) {
    df = new SimpleDateFormat(pattern.replace("h", "H"), Locale.getDefault());
  } else {
    df = new SimpleDateFormat(pattern.replace("H", "h"), Locale.getDefault());
  }
} else {
  // nothing to do or change
}

您当然可以自由地针对可能出现的 k 和 K 改进代码,或者注意文字 h 和 H 的使用(然后解析撇号以在替换方法中忽略这些部分)。

于 2014-04-22T15:06:44.817 回答
1

原始解决方案并不总是有效。我的解决方案更简单优雅:

Locale.US 默认为 12 小时,那么我们设置为 12 小时。

我们检查用户是否检查了 24 小时格式,然后我们将 Locale 设置为 24 小时作为默认值。

boolean twentyFourHourStyle = android.text.format.DateFormat.is24HourFormat(context);
DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.US);
if (twentyFourHourStyle) {
    timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT,Locale.FRANCE);
}

用 3 行解决;)

于 2014-12-02T00:59:28.390 回答
0

确保使用正确的 DateFormat 类。

使用 android.text.format.DateFormat.getTimeFormat(context);

当您在设置中更改时间 12/24 格式时,这会正确更新。

不要使用:java.text.DateFormat。这不起作用。

例如日期 date = new Date(); 字符串时间 = android.text.format.DateFormat.getTimeFormat(context).format(date);

于 2016-04-17T23:58:16.980 回答
0

我意识到这是一个老问题,但我在这里是因为 DateFormat.getTimeInstance(DateFormat.SHORT) 也不适合我。API 26 (Android O) AVD 很好,但它不适用于我的旧 API 16 和 API 19 设备 (Android 4)。所以看起来这个问题只是旧代码的一个错误。我现在使用 android.text.format.DateFormat.getTimeFormat() 结合 android.text.format.DateFormat.is24HourFormat() 来获得正确的时间选择器。这适用于我的 AVD 和我的旧设备。

于 2017-08-26T16:58:50.783 回答