java 如何在java中为SimpleDateFormat应用applyPattern?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7660940/
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
How to apply applyPattern for SimpleDateFormat in java?
提问by Kalimah
I am trying to add a new pattern to the date display but I am not getting the result that I am expecting:
我正在尝试向日期显示添加新模式,但没有得到我期望的结果:
Here is my code:
这是我的代码:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S");
sdf.applyPattern("yyyy-MM-dd");
Date date_out = null;
try {
date_out = sdf.parse(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
((TextView) view.findViewById(R.id.date)).setText(date_out.toString());
I want the output to look something like this: 03 Oct 2011
我希望输出看起来像这样:2011 年 10 月 3 日
However this is the output tat I am getting: Oct 03 00:00:00 GMT+ 11:00 2011
然而,这是我得到的输出:10 月 3 日 00:00:00 GMT+ 11:00 2011
How do I reach the desired output?
我如何达到所需的输出?
EDIT: I solved this code by adding this line:
编辑:我通过添加以下行解决了此代码:
sdf.format(date_out);
instead of setText()
而不是 setText()
回答by palacsint
You have to use two SimpleDateFormat
objects. One for parsing the input and an other one for formatting the parsed Date
object to String.
您必须使用两个SimpleDateFormat
对象。一个用于解析输入,另一个用于将解析的Date
对象格式化为字符串。
final String inputDate = "2011-05-08T11:12:13.0123";
final SimpleDateFormat inputParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S");
Date date_out = null;
try {
date_out = inputParser.parse(inputDate);
} catch (final ParseException e) {
e.printStackTrace();
}
final SimpleDateFormat outputFormatter =
new SimpleDateFormat("dd MMM yyyy", Locale.US);
final String result = outputFormatter.format(date_out);
System.out.println(result);
Output:
输出:
08 May 2011
回答by Martijn Courteaux
Date.toString();
does alwaysformat your String that way. You should a SimpleDateFormat to format the Date
object to the String you want.
Date.toString();
确实总是以这种方式格式化您的字符串。您应该使用 SimpleDateFormat 将Date
对象格式化为您想要的字符串。
The JavaDoc of the Date.toString();
method says:
该Date.toString();
方法的 JavaDoc说:
Converts this Date object to a String of the form:
dow mon dd hh:mm:ss zzz yyyy
将此日期对象转换为以下形式的字符串:
dow mon dd hh:mm:ss zzz yyyy
回答by dacwe
The Date.toString()
method formats the string like that (check the api documentation).
该Date.toString()
方法像这样格式化字符串(检查 api 文档)。
You do not actually need to use applyFormat(...)
in this case. You want to parse one format and output it in another format.
applyFormat(...)
在这种情况下,您实际上不需要使用。您想解析一种格式并以另一种格式输出。
To parse the date (given the string: 2011-10-03") use can use the format
"yyyy-MM-dd"and when you output the
Dateyou want to use
"dd MMM yyyy"`:
解析日期(给定字符串:2011-10-03") use can use the format
“yyyy-MM-dd”and when you output the
日期you want to use
“dd MMM yyyy”`:
public static void main(String[] args) throws ParseException {
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy MMM dd");
Date parsedDate = inputFormat.parse("2011-10-05");
System.out.println(outputFormat.format(parsedDate));
}
Outputs (on US locale):
输出(在美国语言环境中):
2011 Oct 05
回答by duggu
Read below document :-
阅读以下文件:-
http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
hope help u above link.
希望能帮助你以上链接。