SimpleDateFormat的警告,要获得本地格式使用getDateInstance(),getDateTimeInstance(),或getTimeInstance() [英] SimpleDateFormat warning To get local formatting use getDateInstance(), getDateTimeInstance(), or getTimeInstance(),

查看:6584
本文介绍了SimpleDateFormat的警告,要获得本地格式使用getDateInstance(),getDateTimeInstance(),或getTimeInstance()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是否需要担心这个警告?如果我忽略警告?
这是什么警告表示:
为了获得本地格式使用 getDateInstance() getDateTimeInstance() getTimeInstance(),或使用新的SimpleDateFormat(字符串模板,区域设置区域)与例如 Locale.US ASCII 日期。
在下面的code二号线。该应用程序工作正常与code。我想显示日期,例如,二〇一四年十一月十九日

Do i need to be worried about this warning? What if I ignore the warning?
What does this warning mean:
To get local formatting use getDateInstance(), getDateTimeInstance(), or getTimeInstance(), or use new SimpleDateFormat(String template, Locale locale) with for example Locale.US for ASCII dates.
In the 2nd Line of the code below. The App is working fine with the code. I wanted to show date eg, 19 Nov 2014.

public static String getFormattedDate(long calendarTimeInMilliseconds) {
    SimpleDateFormat sdfDate = new SimpleDateFormat("d MMM yyyy");  //ON THIS LINE
    Date now = new Date();
    now.setTime(calendarTimeInMilliseconds);
    String strDate = sdfDate.format(now);
    return strDate;
}

我觉得这是格式化日期正确的方式,这里显示

I think this is a correct way to format date as shown here.

推荐答案

您当前使用的<一个href="http://developer.android.com/reference/java/text/SimpleDateFormat.html#SimpleDateFormat(java.lang.String)">SimpleDateFormat(String)构造函数。这意味着默认的语言环境和语言区域的文档会告诉你,警惕默认的语言环境作为意外输出可以生产在各种系统。

You are currently using the SimpleDateFormat(String) constructor. This implies the default locale and as the Locale documentation tells you, be wary of the default locale as unexpected output can be produced on various systems.

您应该使用的<一个href="http://developer.android.com/reference/java/text/SimpleDateFormat.html#SimpleDateFormat(java.lang.String,%20java.util.Locale)">SimpleDateFormat(String,语言环境)构造。它会采取一个额外的参数 - 区域设置要使用。 如果你想确保输出为机器可读以一致的方式(总是看起来是一样的,不管用户的实际区域设置的),你可以选择的 Locale.US 。如果你不计较机redability,您可以明确地设置为使用<一个href="http://developer.android.com/reference/java/util/Locale.html#getDefault()">Locale.getDefault().

You should instead use the SimpleDateFormat(String, Locale) constructor. It is going to take in an additional parameter - the locale you want to use. If you want to make sure the output is machine-readable in a consistent way (always looks the same, regardless of the actual locale of the user), you can pick Locale.US. If you do not care about machine redability, you can explicitly set it to use Locale.getDefault().

使用那些在你的榜样,code会是这个样子:

Using those on your example code would look something like this:

// for US
SimpleDateFormat sdfDate = new SimpleDateFormat("d MMM yyyy", Locale.US);

// or for default
SimpleDateFormat sdfDate = new SimpleDateFormat("d MMM yyyy",
        Locale.getDefault());

这篇关于SimpleDateFormat的警告,要获得本地格式使用getDateInstance(),getDateTimeInstance(),或getTimeInstance()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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