java SimpleDateFormat 警告要获取本地格式,请使用 getDateInstance()、getDateTimeInstance() 或 getTimeInstance(),
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27023189/
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
SimpleDateFormat warning To get local formatting use getDateInstance(), getDateTimeInstance(), or getTimeInstance(),
提问by Mohammed Ali
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
.
我需要担心这个警告吗?如果我忽略警告怎么办?
这是什么警告表示:
为了让本地格式使用getDateInstance()
,getDateTimeInstance()
或getTimeInstance()
,或使用new SimpleDateFormat(String template, Locale locale)
与例如Locale.US
用于ASCII
日期。
在下面代码的第 2 行中。该应用程序与代码运行良好。我想显示日期,例如,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.
我觉得这是如图所示的正确的方式来格式化日期这里。
回答by Valter Jansons
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 localeas unexpected output can be produced on various systems.
您当前正在使用SimpleDateFormat(String)构造函数。这意味着默认语言环境,正如语言环境文档告诉您的那样,要警惕默认语言环境,因为在各种系统上可能会产生意外的输出。
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().
您应该改用SimpleDateFormat(String, Locale)构造函数。它将接受一个附加参数 - 您要使用的语言环境。如果您想以一致的方式确保输出是机器可读的(始终看起来相同,无论用户的实际语言环境如何),您可以选择Locale.US。如果您不关心机器的可红性,您可以明确地将其设置为使用Locale.getDefault()。
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());
回答by JstnPwll
You can ignore it, but the warning is there to let you know the dates may display unexpectedly for users using a different language. If you want to force the format in your own locale, use Locale.US
(or whichever locale matches your format requirements). Otherwise use one of the getInstance() methods for localized formatting.
您可以忽略它,但警告是为了让您知道对于使用不同语言的用户而言,日期可能会意外显示。如果您想在您自己的语言环境中强制使用格式,请使用Locale.US
(或与您的格式要求相匹配的任何语言环境)。否则使用 getInstance() 方法之一进行本地化格式化。
回答by kuzdu
Same for kotlin
kotlin 也一样
//example long 1372339860
fun getDateTime(unixSeconds: Long): String {
val date = java.util.Date(unixSeconds * 1000L)
val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss z", Locale.getDefault())
return simpleDateFormat.format(date)
}
Call like this getDateTime(1372339860)
像这样打电话 getDateTime(1372339860)
It will return 27.6.2013 15:31:00 GMT +2
The GMT+2
because I'm in Germany and we have summer time. You can use Locale.US
as well.
它将返回27.6.2013 15:31:00 GMT +2
TheGMT+2
因为我在德国并且我们有夏季时间。您也可以使用Locale.US
。