在java中将日期格式化为“日月数,年”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19306972/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:55:02  来源:igfitidea点击:

format date in java as "Day Month number, year"

javadate

提问by Cote Mounyo

The following code prints the date "Thu, 10 Oct 2013"

以下代码打印日期“2013 年 10 月 10 日星期四”

Date date = new Date(timeInMilliSec);
SimpleDateFormat df2 = new SimpleDateFormat("EEE, dd MMM yyyy");
String dateText = df2.format(date);
return dateText;

How do I get "Thu Oct 21, 2013"?

我如何获得“2013 年 10 月 21 日星期四”?

采纳答案by libik

In this line you define how your output looks like :

在这一行中,您定义了输出的样子:

SimpleDateFormat df2 = new SimpleDateFormat("EEE, dd MMM yyyy");

Changing the string is changing the format, at your example you need this:

更改字符串正在更改格式,在您的示例中,您需要:

SimpleDateFormat df2 = new SimpleDateFormat("EEE MMM dd, yyyy");

If you look to documentation, you can see what each character means. If character does not mean anything, it is parsed to the output as it is, otherwise it is changed to things like "day in month in -dd- input"

如果您查看文档,您可以看到每个字符的含义。如果字符没有任何意义,则将其按原样解析为输出,否则将更改为“-dd-输入中的月中日”之类的内容

Documentation of SimpleDateFormat

SimpleDateFormat 的文档