java中有没有一种日期格式可以显示星期几?
我知道日期格式,例如"yyyy-mm-dd"
- 以 2011-02-26
格式显示日期 “yyyy-MMM-dd”
- 以 2011-FEB-26
格式显示日期,
用于例如:
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy/MMM/dd ");
我想要一种可以帮助我显示日期的格式诸如 2011-02-MON
之类的星期或其他任何内容。我只想以字符形式显示星期几以及月份和年份。你能告诉我这样的格式吗?
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
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这应该显示“星期二”:
这应该显示“星期二”:
这应该显示“T”:
所以你的具体示例是:
This should display 'Tue':
This should display 'Tuesday':
This should display 'T':
So your specific example would be:
是的 - 'E' 可以解决问题
http://download .oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
Yep - 'E' does the trick
http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
tl;dr
java.time
现代方法使用 java.time 类。
请注意我们如何指定
Locale
(例如Locale.CANADA_FRENCH
)来确定用于翻译日期名称的人类语言。ISO 8601
顺便说一句,您可能对标准 ISO 8601 周编号方案感兴趣:
yyyy-Www-d
。第 1 周是该日历年的第一个星期四。一周从星期一开始。一年有 52 周或 53 周。日历年的最后/前几天可能会落在下一个/上一周的年份中。
末尾的个位数表示星期几,1-7 表示周一至周日。
将 ThreeTen-Extra 库类添加到
YearWeek
类的项目中。关于 java.time
java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧遗留日期时间类,例如
java.util.Date
,日历
, & ;SimpleDateFormat
。Joda-Time 项目,现已在 维护模式,建议迁移到 java.time 类。
要了解更多信息,请参阅 Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范为 JSR 310。
从哪里获取 java.time 类?
ThreeTen-Extra 项目通过附加类扩展了 java.time 。该项目是 java.time 未来可能添加的内容的试验场。您可能会在这里找到一些有用的类,例如
间隔
,YearWeek
,<代码>YearQuarter,以及更多 。tl;dr
java.time
The modern approach uses the java.time classes.
Note how we specify a
Locale
such asLocale.CANADA_FRENCH
to determine the human language used to translate the name of the day.ISO 8601
By the way, you may be interested in the standard ISO 8601 week numbering scheme:
yyyy-Www-d
.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.
The single digit on the end is day-of-week, 1-7 for Monday-Sunday.
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
.The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
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.EEE 代表星期几,例如星期四显示为 Thu。
EEE stands for day of week for example Thursday is displayed as Thu.
使用“E”
请参阅有关日期和时间模式的部分:
SimpleDateFormat 的 JavaDocs
Use "E"
See the section on Date and Time Patterns:
JavaDocs for SimpleDateFormat
我知道问题是如何将星期几作为字符串(例如短名称),但是对于任何正在寻找数字星期几的人(就像我一样),您可以使用新的“u”格式字符串,支持从 Java 7 开始。例如:
返回今天的星期索引,即:1 = 星期一,2 = 星期二,...,7 = 星期日。
I know the question is about getting the day of week as string (e.g. the short name), but for anybody who is looking for the numeric day of week (as I was), you can use the new "u" format string, supported since Java 7. For example:
returns today's day-of-week index, namely: 1 = Monday, 2 = Tuesday, ..., 7 = Sunday.