- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我是一名法国 Android 开发人员,因此使用 Locale.getDefault()
会导致我的 DateFormat
使用 24 小时模式。但是,当我通过设置菜单将我的设备手动设置为 12 小时模式时,DateFormat
继续以 24 小时格式运行。
相反,TimePicker
是根据我自己的12/24小时制设置的。
有什么方法可以使 DateFormat
的行为与 TimePicker
的行为相同?
编辑:
这是我的 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
也将被正确设置。
最佳答案
如果您在 SimpleDateFormat
中指定了一个模式,那么您已经修复了 12/24 小时模式,模式符号“h”(1-12) 的情况下为 12 小时模式或模式符号“H”(0-23) 时的 24 小时模式。选项“k”和“K”类似,只是范围略有不同。
也就是说,指定模式可以让您的格式独立于设备设置!
另一种方法是使用 DateFormat.getDateTimeInstance()这使得时间样式依赖于系统区域设置(如果 Locale.getDefault()
可能会改变 - 或者你必须部署一种机制如何询问当前设备区域设置,然后在 Android-Java 中设置 Locale.setDefault()
).
Android 专用 的另一个想法是使用字符串常量 TIME_12_24 直接询问系统设置。然后根据此设置指定模式。这似乎也可以通过特殊方法实现 DateFormat.is24HourFormat() (请注意,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-method 中忽略这些部分)。
关于android - 12/24 小时模式冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23222199/
我的工作是将每条记录写入 Hadoop 映射中的 DynamoDB。 我无法使用具有 httpclient-4.2.5.jar 和 httpcore-4.2.5.jar 的 Hadoop 2.6 运行
我在 JavaScript 中嵌套了循环开关,例如: for (var i = 0; i < checkBoxIds.length; i++) { if ($('#' + checkB
我有一个文件夹被重命名的分支。现在我正在尝试合并从主干到这个分支的更改,但是在主干中修改的文件仍然在原始文件夹名称下,因此 svn 引发了树冲突。我该如何解决这个问题? 该文件夹已使用 svn ren
我在使用 jquery 暴露插件和 Masked 输入插件时遇到了问题。问题是它们都占用 $.mask 函数,从而导致冲突。但我非常需要这两个插件一起工作。我会将其中一个中的 $.mask 重命名为.
我正在尝试为我所有的 INPUT 元素做一个简单的 :focus 效果,就像这样: INPUT:focus { border-color: orange; } 这很好用,直到我将这段 CSS 添加到样
我是 javascript 的新手。 在 wordpress 上工作,我在自定义 js 中为我的主题输入了以下函数: document.getElementsByName("empty_cart")[
为什么有些 javascript 会与其他的发生冲突?我的意思是我一直在为图片库使用 javascript 代码,然后尝试在 jquery 中获取文本水印。为什么在使用 jquery 之后,画廊完全消
是否可以根据已知的输入值创建 MD5 碰撞? 所以例如我有输入字符串 abc与 MD5 900150983cd24fb0d6963f7d28e17f72 . 现在我想将字节添加到字符串 def获得相同
我在我的项目中使用原型(prototype): NodeParser.prototype.getChildren = function(parentContainer) { return fl
根据我对 merge 冲突的理解,当两个人更改了同一文件和/或修改了该文件中的同一行时,就会发生 merge 冲突。所以当我做一个 git pull origin master 我期望 merge 冲
iPad 上 Mobile Safari 中的 HTML5 模板。带 iScroll 的 Div 工作正常。 if/else 语句中还包含一个 jQuery 函数。该函数测试用户是否在 iScroll
我一直在尝试使 2 个脚本(1 个 mootol 和 1 个 jquery)在同一页面上工作,但没有成功。我一直在研究许多论坛等,但我仍然无法使这两个脚本同时工作。 这就是它在我的标题中的样子: /w
我想克隆带标题的问号。一切正常,但是当我将鼠标悬停在新问号上时,第一个问号上会出现工具提示。有什么想法吗? 我正在使用 jQuery 和醉酒的工具提示。 Demo here click here
好吧,我已经在一个 friend 的网站上工作了一段时间了。我的编码技能......值得怀疑,而且我遇到了很多问题。 目前我网站上的 jQuery 停止工作了,我找不到原因,并且我已尽一切努力让它工作
我想使用一个文件来保存所有#define 和常量:示例 #ifndef CONSTANTS_H_ #define CONSTANTS_H_ #include //OVERALL DEFS
我在我的表单中的所有 HTML 下都有一些验证码,这似乎阻止了我的复选框验证码的工作,一旦我在我的 HTML 下的代码周围添加/* */(使其不活动),我就得出了这个结论) 复选框验证代码开始正常工作
我是新手。我的页面上有两个 javascript/jquery。如果我使用第一个脚本,第二个脚本将不起作用。如果我删除第一个脚本,第二个脚本就可以正常工作。我不知道如何解决这个问题。我知道有一个“无冲
我是 Bison 新手,在处理 Shift/Reduce 冲突时遇到了麻烦... 我正在为C语言编写语法规则:ID是标识变量的标记,我编写此规则是为了确保即使将标识符写在括号中,也可以考虑它。 id
我目前正在使用 LibGdx 开发我的第一个 Android 游戏项目。这是一款 2D 迷宫游戏,您可以使用触摸输入从其中一个入口到其中一个导出“画出”一条线。世界本身是一个 TiledMap,目前仅
我正在尝试通过 javascript 完成表单验证,并在 #form# 标记中放置一个要执行的 .py 文件操作。但问题是,当我提交数据后,它不会重定向到 .py 操作。所以我摆脱了 preventd
我是一名优秀的程序员,十分优秀!