Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 12/24小时模式冲突_Android_Simpledateformat_Android Timepicker - Fatal编程技术网

Android 12/24小时模式冲突

Android 12/24小时模式冲突,android,simpledateformat,android-timepicker,Android,Simpledateformat,Android Timepicker,我是一名法国Android开发人员,因此使用Locale.getDefault()会导致我的DateFormat使用24小时模式。但是,当我通过设置菜单手动将设备设置为12小时模式时,DateFormat将以24小时格式运行 相反,TimePickers是根据我自己的12/24小时设置设置的 有没有办法使DateFormats的行为方式与TimePickers相同 编辑: 这是我的DateFormat声明: timeFormat = DateFormat.getTimeInstance(Date

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

相反,
TimePicker
s是根据我自己的12/24小时设置设置的

有没有办法使
DateFormat
s的行为方式与
TimePicker
s相同

编辑:

这是我的
DateFormat
声明:

timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault());
在这里,我将时间选择器设置为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小时格式。并且
时间选择器也将被正确设置。

如果您在
SimpleDataFormat
中指定了一个模式,那么您已经修复了12/24小时模式,模式符号“h”(1-12)为12小时模式,模式符号“h”(0-23)为24小时模式。备选方案“k”和“k”相似,但范围略有不同

这就是说,指定一个模式使您的格式独立于设备设置

另一种方法是使用使时间样式依赖于系统区域设置的方法(如果
locale.getDefault()
可能会更改,或者您必须部署一种机制来询问当前设备区域设置,然后在Android Java
locale.setDefault()
)中进行设置

Android特有的另一个想法是使用字符串常量直接询问系统设置,然后指定依赖于此设置的模式。这似乎也可以通过特殊的方法实现(请注意,Android有两个名为
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(然后在replace方法中解析撇号以忽略这些部分)。

原始解决方案并不总是很好地工作。我的解决方案更简单、更优雅:

默认情况下,Locale.US为12小时,然后我们将其设置为12小时

我们检查用户是否选择了24小时格式,然后设置一个默认为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行代码求解;)

确保使用正确的DateFormat类

使用android.text.format.DateFormat.getTimeFormat(上下文)

当您在“设置”中更改“时间12/24”格式时,此选项将正确更新

不要使用:java.text.DateFormat。这是行不通的

e、 g.日期=新日期();
String time=android.text.format.DateFormat.getTimeFormat(context.format(date))

我意识到这是一个老问题,但我在这里是因为DateFormat.getTimeInstance(DateFormat.SHORT)也不适合我。使用API26(安卓O)AVD还可以,但使用我的旧API16和API19设备(安卓4)就不行了。所以看起来问题只是旧代码的一个bug。我现在使用android.text.format.DateFormat.getTimeFormat()和android.text.format.DateFormat.is24HourFormat()组合来获取正确的时间选择器。这在我的AVD和旧设备上都有效。

您是否指定自己的格式?注意“hh”和“hh”之间的区别,请发布一些代码。设置SimpleDateFormat的部分。如果可以,请发布用于SimpleDateFormat的字符串,这会很有帮助。哎呀,我的错。它不是
SimpleDateFormat
,而是
DateFormat
。只是编辑以更正此问题。不幸的是,我没有指定任何模式。我正在使用
java.text.DateFormat
常量,如上所示。@RichouHunter我现在更新了我的答案,给出了一个可能的解决方案,因为大多数
DateFormat
-对象在内部实际上都是
SimpleDateFormat
-objects.Ok,我不知道
DateFormat
是内部
SimpleDateFormat
。但是,在24小时格式的情况下,我又添加了一个
replace()
语句来删除“AM/PM”标记。非常感谢@RichouHunter是AM/PM标记器的好捕获物。提示:您应该研究为不同地区创建的模式(来自android repository,最初来自CLDR),以获得关于replace-mechanism的更多安全性。除了“HH:mm”和“HH:mm a”之外,还有其他时间格式吗?这似乎有点困扰我想象其他方式来格式化时间…请把你的答案解释。Welkom to SO!请在回答时解释一下,这是如何解决最初的问题的:“有没有办法使日期格式与计时器的行为相同?”。