android.text.format.DateFormat" HH"不能识别等与java.text.SimpleDateFormat的 [英] android.text.format.DateFormat "HH" is not recognized like with java.text.SimpleDateFormat

查看:264
本文介绍了android.text.format.DateFormat" HH"不能识别等与java.text.SimpleDateFormat的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 android.text.format.DateFormat 的HH的标志,这是PTED为文字HH跨$ P $。但是,当我使用 java.text.SimpleDateFormat的这是PTED为2位数的小时跨$ P $。为什么不同?

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?

我不是在寻找一个替代的工作(我已经知道我要使用 KK 而不是 HH )。我只是好奇,为什么HH不被认可。

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.

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
}

输出是:

Android: 26-05-2013 HH:36:34 
Java:    26-05-2013 22:36:34

我希望无论是 26-05-2013 22时36分34秒

难道Android的日期格式有错误吗?

Does Android's DateFormat have a bug?

Java的SimpleDateFormat的接受这些:

Java's SimpleDateFormat accepts these:

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

所以它出现在Android开发者决定改变的含义 K 并在他们的日期格式的功能就相当于SimpleDateFormat的 ^ h ,因为他们明确地说,在他们的文档页面

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:

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?

推荐答案

我知道你已经接受了一个答案了,但是只是为了解释这个完全给你...

I understand you have accepted an answer already but just to explain this fully to you...

在<一个href="https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/text/format/DateFormat.java"相对=nofollow>来源$ C ​​$下DateFormat.java ...

在{@ code画幅}在这个类中的方法实行统一code的一个子集    UTS#35 模式。   目前此类支持的子集包括以下格式字符:   {@ ​​code acdEHhLKkLMmsyz}。 截至API级别17,只有{@ code adEhkMmszy}的支持。   注意,这个类错误地实现{@ codeK},好像它是{@ $ C $] C H}为了向后   兼容性。

The {@code format} methods in this class implement a subset of Unicode UTS #35 patterns. The subset currently supported by this class includes the following format characters: {@code acdEHhLKkLMmsyz}. Up to API level 17, only {@code adEhkMmszy} were supported. Note that this class incorrectly implements {@code k} as if it were {@code H} for backwards compatibility.

请注意我以粗体标记的部分。

Note the part I have marked in bold.

源我链接到已更新为允许使用的h但它不是一般的释放,但(API 17是目前Android版本,不支持H)。

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).

后来的来源,在声明的格式字符常量的阶段,有此评论...

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;

这篇关于android.text.format.DateFormat&QUOT; HH&QUOT;不能识别等与java.text.SimpleDateFormat的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆