android.text.format.DateFormat "HH" 不像 java.text.SimpleDateFormat 那样被识别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16763968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android.text.format.DateFormat "HH" is not recognized like with java.text.SimpleDateFormat
提问by Alejandro Colorado
When I use the "HH" flag in android.text.format.DateFormat
, it is interpreted as a literal "HH". But when I use java.text.SimpleDateFormat
it is interpreted as the 2 digit Hour. Why are they different?
当我在 中使用“HH”标志时android.text.format.DateFormat
,它被解释为文字“HH”。但是当我使用java.text.SimpleDateFormat
它时,它被解释为 2 位小时。他们为什么不同?
I'm not looking for a working alternative (I already know I have to use kk
instead of HH
). I'm just curious why "HH" isn't recognized.
我不是在寻找可行的替代方案(我已经知道我必须使用kk
代替HH
)。我只是好奇为什么“HH”不被识别。
Java example:
Java示例:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Calendar calendar = Calendar.getInstance();
String dateJava = new java.text.SimpleDateFormat(
"dd-MM-yyyy HH:mm:ss").format(calendar.getTime());
String dateAndroid = android.text.format.DateFormat.format(
"dd-MM-yyyy HH:mm:ss", calendar).toString();
TextView tvAndroid = (TextView) findViewById(R.id.tvAndroid);
TextView tvJava = (TextView) findViewById(R.id.tvJava);
tvAndroid.setText("Android: " + dateAndroid); //prints 26-05-2013 HH:36:34
tvJava.setText("Java: " + dateJava); //prints 26-05-2013 22:36:34
}
Output is:
输出是:
Android: 26-05-2013 HH:36:34
Java: 26-05-2013 22:36:34
I expect both to be 26-05-2013 22:36:34
我希望两者都是 26-05-2013 22:36:34
Does Android's DateFormat have a bug?
Android 的 DateFormat 有错误吗?
Java's SimpleDateFormat accepts these:
Java 的 SimpleDateFormat 接受这些:
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
So it appears the Android developers decided to change the meaning of k
and in their DateFormat function it is equivalent to the SimpleDateFormat H
as they explicitly say in their documentation page:
因此,Android 开发人员似乎决定更改k
DateFormat 函数的含义,它等同于 SimpleDateFormat,H
正如他们在文档页面中明确指出的那样:
This constant was deprecated in API level 18. Use a literal 'H' (for
compatibility with SimpleDateFormat and Unicode) or 'k' (for compatibility
with Android releases up to and including Jelly Bean MR-1) instead. Note
that the two are incompatible.
What is the reason for this?
这是什么原因?
采纳答案by Squonk
I understand you have accepted an answer already but just to explain this fully to you...
我知道您已经接受了一个答案,但只是为了向您充分解释这一点......
From the source code for DateFormat.java...
从DateFormat.java的源代码...
The
format
methods in this class implement a subset of Unicode UTS #35patterns. The subset currently supported by this class includes the following format characters:acdEHhLKkLMmsyz
. Up to API level 17, onlyadEhkMmszy
were supported. Note that this class incorrectly implementsk
as if it wereH
for backwards compatibility.
format
此类中的方法实现了 Unicode UTS #35模式的子集 。此类当前支持的子集包括以下格式字符:acdEHhLKkLMmsyz
. 仅adEhkMmszy
支持API 级别 17 。请注意,这个类错误地实现k
,好像它是H
为了向后兼容。
Note the part I have marked in bold.
请注意我用粗体标记的部分。
The source I linked to has been updated to allow the use of H but it isn't on general release yet (API 17 is the current release of Android and doesn't support H).
我链接到的源已更新以允许使用 H,但它还没有正式发布(API 17 是 Android 的当前版本,不支持 H)。
Later in the source, at the stage of declaring the format character constants, there is this comment...
后来在源码中,在声明格式字符常量的阶段,有这样的注释……
/**
* @deprecated Use a literal {@code 'H'} (for compatibility with {@link SimpleDateFormat}
* and Unicode) or {@code 'k'} (for compatibility with Android releases up to and including
* Jelly Bean MR-1) instead. Note that the two are incompatible.
*/
@Deprecated
public static final char HOUR_OF_DAY = 'k';
...and later during character replacement...
......后来在字符替换过程中......
case 'H': // hour in day (0-23)
case 'k': // hour in day (1-24) [but see note below]
{
int hour = inDate.get(Calendar.HOUR_OF_DAY);
// Historically on Android 'k' was interpreted as 'H', which wasn't
// implemented, so pretty much all callers that want to format 24-hour
// times are abusing 'k'. http://b/8359981.
if (false && c == 'k' && hour == 0) {
hour = 24;
}
replacement = zeroPad(hour, count);
}
break;
回答by Brian Roach
Because ... it's not the same thing and it's behaving as the documentation states?
因为......它不是一回事,它的行为与文档说明的一样?
From the Documentation for android.text.format.DateFormat
来自android.text.format.DateFormat的文档
This class only supports a subset of the full Unicode specification. Use SimpleDateFormat if you need more.
此类仅支持完整 Unicode 规范的一个子集。如果您需要更多,请使用 SimpleDateFormat。
But if you read the docs further:
但是,如果您进一步阅读文档:
public static final char HOUR_OF_DAY
This designator indicates the hour of the day in 24 hour format. Example for 3pm: k -> 15 Examples for midnight: k -> 0 kk -> 00
公共静态最终字符 HOUR_OF_DAY
此指示符以 24 小时格式指示一天中的小时。下午 3 点的示例:k -> 15 午夜的示例:k -> 0 kk -> 00
So ... using that class, it'd be kk
instead of HH
所以......使用那个类,它会kk
代替HH
回答by msh
For android.text.format.DateFormat you designate Hour in day as kk
like this:
对于 android.text.format.DateFormat,您可以kk
像这样指定一天中的小时:
String dateAndroid = android.text.format.DateFormat.format(
"dd-MM-yyyy kk:mm:ss", calendar).toString();
For java.text.SimpleDateFormat you designate hour in day as HH
.
对于 java.text.SimpleDateFormat,您将一天中的小时指定为HH
.
H hour in day (0-23)
一天中的 H 小时 (0-23)
Documentation for android.text.format.DateFormat:
android.text.format.DateFormat 的文档:
public static final char HOUR_OF_DAY
This designator indicates the hour of the day in 24 hour format. Example for 3pm: k -> 15 Examples for midnight: k -> 0 kk -> 00
公共静态最终字符 HOUR_OF_DAY
此指示符以 24 小时格式指示一天中的小时。下午 3 点的示例:k -> 15 午夜的示例:k -> 0 kk -> 00
回答by AlexR
I have never programmed for Android. I googled the DateFormat javadocand saw there the following examples:
我从来没有为Android编程。我用谷歌搜索了DateFormat javadoc并在那里看到了以下示例:
Examples for April 6, 1970 at 3:23am:
"MM/dd/yy h:mmaa" -> "04/06/70 3:23am"
"MMM dd, yyyy h:mmaa" -> "Apr 6, 1970 3:23am"
"MMMM dd, yyyy h:mmaa" -> "April 6, 1970 3:23am"
"E, MMMM dd, yyyy h:mmaa" -> "Mon, April 6, 1970 3:23am&
"EEEE, MMMM dd, yyyy h:mmaa" -> "Monday, April 6, 1970 3:23am"
"'Noteworthy day: 'M/d/yy" -> "Noteworthy day: 4/6/70"
The "hour" is marked using small letter h
in opposite to SimpleDateFormat
where capital letter is used for this purpose.
“小时”用小写字母标记h
,与SimpleDateFormat
用于此目的的大写字母相反。
回答by Alex
This work for all Android 4.0+ and for two date time format.
这适用于所有 Android 4.0+ 和两种日期时间格式。
Use java.text.SimpleDateFormat.
使用 java.text.SimpleDateFormat。
Work example:
工作示例:
24 hour format use this date pattern = "dd-MM-yyyy HH:mm";
24 小时格式使用此日期模式 = " dd-MM-yyyy HH:mm";
12 hour format use this date pattern = "dd-MM-yyyy hh:mm a";
12 小时格式使用此日期模式 = " dd-MM-yyyy hh:mm a";
public static String getDateAsString(Date date, String pattern) {
Locale locale = new Locale("EN");
SimpleDateFormat sdf = null;
try {
sdf = new SimpleDateFormat(pattern, locale);
return sdf.format(date);
} catch (IllegalArgumentException ex) {
// default format
sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm", locale);
return sdf.format(date);
}
}