I know of date formats such as "yyyy-mm-dd"
-which displays date in format 2011-02-26
"yyyy-MMM-dd"
-which displays date in format 2011-FEB-26
我知道日期格式,比如“yyyyy -mm-dd”,它以2011-02-26格式显示日期,“yyyyyy -mm-dd”以2011-FEB-26格式显示日期
to be used in eg:
在…中使用如:
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy/MMM/dd ");
I want a format which would help me display the day of the week like 2011-02-MON
or anything. I just want the day of the week to be displayed in characters with the month and the year. Can you tell me of a format like this?
我想要一种能帮助我显示一周一天的格式,比如2011-02-MON之类的。我只是想把一周中的一天用月份和年份的字符显示出来。你能告诉我一种像这样的格式吗?
5 个解决方案
#1
217
This should display 'Tue':
这应该显示“星期二”:
new SimpleDateFormat("EEE").format(new Date());
This should display 'Tuesday':
这应该显示“周二”:
new SimpleDateFormat("EEEE").format(new Date());
This should display 'T':
这应该显示“T”:
new SimpleDateFormat("EEEEE").format(new Date());
So your specific example would be:
所以你的具体例子是:
new SimpleDateFormat("yyyy-MM-EEE").format(new Date());
#2
14
Yep - 'E' does the trick
是的,E可以
http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
Date date = new Date();
DateFormat df = new SimpleDateFormat("yyyy-MM-E");
System.out.println(df.format(date));
#3
4
SimpleDateFormat sdf=new SimpleDateFormat("EEE");
EEE stands for day of week for example Thursday is displayed as Thu.
EEE代表一周的时间,例如周四显示为Thu。
For complete program you can view the given below link
对于完整的程序,你可以查看下面的链接
http://java-interview-questions.com/how-to-display-day-of-week-in-java/
http://java-interview-questions.com/how-to-display-day-of-week-in-java/
#4
3
Use "E"
使用“E”
See the section on Date and Time Patterns:
见日期和时间模式一节:
的JavaDocs SimpleDateFormat
#5
2
tl;dr
LocalDate.of( 2018 , Month.JANUARY , 23 )
.format( DateTimeFormatter.ofPattern( “uuuu-MM-EEE” , Locale.US ) )
java.time
The modern approach uses the java.time classes.
现代的方法使用java。时间类。
LocalDate ld = LocalDate.of( 2018 , Month.JANUARY , 23 ) ;
Note how we specify a Locale
such as Locale.CANADA_FRENCH
to determine the human language used to translate the name of the day.
注意我们如何指定语言环境(比如语言环境)。确定法语中用来翻译人名的人类语言。
DateTimeFormatter f = DateTimeFormatter.ofPattern( “uuuu-MM-EEE” , Locale.US ) ;
String output = ld.format( f ) ;
ISO 8601
By the way, you may be interested in the standard ISO 8601 week numbering scheme: yyyy-Www-d
.
顺便说一下,您可能对标准的ISO 8601周编号方案感兴趣:yyyyy - www -d。
2018-W01-2
2018 - w01 - 2
Week # 1 has the first Thursday of the calendar-year. Week starts on a Monday. A year has either 52 or 53 weeks. The last/first few days of a calendar-year may land in the next/previous week-based-year.
第一周是日历年的第一个星期四。星期从星期一开始。一年有52周或53周。日历年的最后/前几天可能会在下一个/前一个星期开始。
The single digit on the end is day-of-week, 1-7 for Monday-Sunday.
最后的个位数是周一-周日的1-7。
Add the ThreeTen-Extra library class to your project for the YearWeek
class.
在学年课程的项目中增加三个额外的图书馆类。
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
java。时间框架构建在Java 8和之后。这些类取代了麻烦的旧遗留日期时间类(如java.util)。日期,日历,& SimpleDateFormat。
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
现在处于维护模式的Joda-Time项目建议迁移到java。时间类。
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
要了解更多,请参阅Oracle教程。和搜索堆栈溢出为许多例子和解释。规范是JSR 310。
Where to obtain the java.time classes?
在哪里获得java。时间类?
-
Java SE 8, Java SE 9, and later
- Built-in.
- 内置的。
- Part of the standard Java API with a bundled implementation.
- 带有捆绑实现的标准Java API的一部分。
- Java 9 adds some minor features and fixes.
- Java 9添加了一些次要的特性和修复。
- Java SE 8, Java SE 9,以及后来的内置。带有捆绑实现的标准Java API的一部分。Java 9添加了一些次要的特性和修复。
-
Java SE 6 and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- 大部分java。时间功能在三个回端移植到Java 6和7。
- Java SE 6和Java SE 7大部分的Java。时间功能在三个回端移植到Java 6和7。
-
Android
- Later versions of Android bundle implementations of the java.time classes.
- java的Android bundle实现的后续版本。时间类。
- For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
- 对于早期的Android, ThreeTenABP项目适应ThreeTen-Backport(上面提到过)。看到如何使用ThreeTenABP ....
- Android后期版本的java捆绑包实现。时间类。对于早期的Android, ThreeTenABP项目适应ThreeTen-Backport(上面提到过)。看到如何使用ThreeTenABP ....
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
三个额外的项目扩展了java。时间和额外的类。这个项目是java.time未来可能增加的一个试验场。你可能会在这里找到一些有用的课程,比如时间间隔、YearWeek、YearQuarter等等。
#1
217
This should display 'Tue':
这应该显示“星期二”:
new SimpleDateFormat("EEE").format(new Date());
This should display 'Tuesday':
这应该显示“周二”:
new SimpleDateFormat("EEEE").format(new Date());
This should display 'T':
这应该显示“T”:
new SimpleDateFormat("EEEEE").format(new Date());
So your specific example would be:
所以你的具体例子是:
new SimpleDateFormat("yyyy-MM-EEE").format(new Date());
#2
14
Yep - 'E' does the trick
是的,E可以
http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
Date date = new Date();
DateFormat df = new SimpleDateFormat("yyyy-MM-E");
System.out.println(df.format(date));
#3
4
SimpleDateFormat sdf=new SimpleDateFormat("EEE");
EEE stands for day of week for example Thursday is displayed as Thu.
EEE代表一周的时间,例如周四显示为Thu。
For complete program you can view the given below link
对于完整的程序,你可以查看下面的链接
http://java-interview-questions.com/how-to-display-day-of-week-in-java/
http://java-interview-questions.com/how-to-display-day-of-week-in-java/
#4
3
Use "E"
使用“E”
See the section on Date and Time Patterns:
见日期和时间模式一节:
的JavaDocs SimpleDateFormat
#5
2
tl;dr
LocalDate.of( 2018 , Month.JANUARY , 23 )
.format( DateTimeFormatter.ofPattern( “uuuu-MM-EEE” , Locale.US ) )
java.time
The modern approach uses the java.time classes.
现代的方法使用java。时间类。
LocalDate ld = LocalDate.of( 2018 , Month.JANUARY , 23 ) ;
Note how we specify a Locale
such as Locale.CANADA_FRENCH
to determine the human language used to translate the name of the day.
注意我们如何指定语言环境(比如语言环境)。确定法语中用来翻译人名的人类语言。
DateTimeFormatter f = DateTimeFormatter.ofPattern( “uuuu-MM-EEE” , Locale.US ) ;
String output = ld.format( f ) ;
ISO 8601
By the way, you may be interested in the standard ISO 8601 week numbering scheme: yyyy-Www-d
.
顺便说一下,您可能对标准的ISO 8601周编号方案感兴趣:yyyyy - www -d。
2018-W01-2
2018 - w01 - 2
Week # 1 has the first Thursday of the calendar-year. Week starts on a Monday. A year has either 52 or 53 weeks. The last/first few days of a calendar-year may land in the next/previous week-based-year.
第一周是日历年的第一个星期四。星期从星期一开始。一年有52周或53周。日历年的最后/前几天可能会在下一个/前一个星期开始。
The single digit on the end is day-of-week, 1-7 for Monday-Sunday.
最后的个位数是周一-周日的1-7。
Add the ThreeTen-Extra library class to your project for the YearWeek
class.
在学年课程的项目中增加三个额外的图书馆类。
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
java。时间框架构建在Java 8和之后。这些类取代了麻烦的旧遗留日期时间类(如java.util)。日期,日历,& SimpleDateFormat。
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
现在处于维护模式的Joda-Time项目建议迁移到java。时间类。
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
要了解更多,请参阅Oracle教程。和搜索堆栈溢出为许多例子和解释。规范是JSR 310。
Where to obtain the java.time classes?
在哪里获得java。时间类?
-
Java SE 8, Java SE 9, and later
- Built-in.
- 内置的。
- Part of the standard Java API with a bundled implementation.
- 带有捆绑实现的标准Java API的一部分。
- Java 9 adds some minor features and fixes.
- Java 9添加了一些次要的特性和修复。
- Java SE 8, Java SE 9,以及后来的内置。带有捆绑实现的标准Java API的一部分。Java 9添加了一些次要的特性和修复。
-
Java SE 6 and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- 大部分java。时间功能在三个回端移植到Java 6和7。
- Java SE 6和Java SE 7大部分的Java。时间功能在三个回端移植到Java 6和7。
-
Android
- Later versions of Android bundle implementations of the java.time classes.
- java的Android bundle实现的后续版本。时间类。
- For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
- 对于早期的Android, ThreeTenABP项目适应ThreeTen-Backport(上面提到过)。看到如何使用ThreeTenABP ....
- Android后期版本的java捆绑包实现。时间类。对于早期的Android, ThreeTenABP项目适应ThreeTen-Backport(上面提到过)。看到如何使用ThreeTenABP ....
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
三个额外的项目扩展了java。时间和额外的类。这个项目是java.time未来可能增加的一个试验场。你可能会在这里找到一些有用的课程,比如时间间隔、YearWeek、YearQuarter等等。