java中有没有一种日期格式可以显示星期几?

发布于 2024-10-19 06:43:57 字数 375 浏览 6 评论 0原文

我知道日期格式,例如
"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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

伴我老 2024-10-26 06:43:57

这应该显示“星期二”:

new SimpleDateFormat("EEE").format(new Date());

这应该显示“星期二”:

new SimpleDateFormat("EEEE").format(new Date());

这应该显示“T”:

new SimpleDateFormat("EEEEE").format(new Date());

所以你的具体示例是:

new SimpleDateFormat("yyyy-MM-EEE").format(new Date());

This should display 'Tue':

new SimpleDateFormat("EEE").format(new Date());

This should display 'Tuesday':

new SimpleDateFormat("EEEE").format(new Date());

This should display 'T':

new SimpleDateFormat("EEEEE").format(new Date());

So your specific example would be:

new SimpleDateFormat("yyyy-MM-EEE").format(new Date());
东北女汉子 2024-10-26 06:43:57

是的 - 'E' 可以解决问题

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));

Yep - 'E' does the trick

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));
·深蓝 2024-10-26 06:43:57

tl;dr

LocalDate.of( 2018 , Month.JANUARY , 23 )
         .format( DateTimeFormatter.ofPattern( "uuuu-MM-EEE" , Locale.US )  )

java.time

现代方法使用 java.time 类。

LocalDate ld = LocalDate.of( 2018 , Month.JANUARY , 23 ) ;

请注意我们如何指定 Locale(例如 Locale.CANADA_FRENCH)来确定用于翻译日期名称的人类语言。

DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-EEE" , Locale.US ) ;
String output = ld.format( f ) ;

ISO 8601

顺便说一句,您可能对标准 ISO 8601 周编号方案感兴趣:yyyy-Www-d

2018-W01-2

第 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

LocalDate.of( 2018 , Month.JANUARY , 23 )
         .format( DateTimeFormatter.ofPattern( "uuuu-MM-EEE" , Locale.US )  )

java.time

The modern approach uses the java.time classes.

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.

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.

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.

兲鉂ぱ嘚淚 2024-10-26 06:43:57
SimpleDateFormat sdf=new SimpleDateFormat("EEE");

EEE 代表星期几,例如星期四显示为 Thu。

SimpleDateFormat sdf=new SimpleDateFormat("EEE");

EEE stands for day of week for example Thursday is displayed as Thu.

紫﹏色ふ单纯 2024-10-26 06:43:57

使用“E”

请参阅有关日期和时间模式的部分:

SimpleDateFormat 的 JavaDocs

Use "E"

See the section on Date and Time Patterns:

JavaDocs for SimpleDateFormat

初与友歌 2024-10-26 06:43:57

我知道问题是如何将星期几作为字符串(例如短名称),但是对于任何正在寻找数字星期几的人(就像我一样),您可以使用新的“u”格式字符串,支持从 Java 7 开始。例如:

new SimpleDateFormat("u").format(new Date());

返回今天的星期索引,即: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:

new SimpleDateFormat("u").format(new Date());

returns today's day-of-week index, namely: 1 = Monday, 2 = Tuesday, ..., 7 = Sunday.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文