Java:获取当前星期几的值
目前,我正在创建一个 java 日程应用程序,我想知道如何在使用 Calendar 类获取一周的第一天的同时找到一周的当前日期。
这是我在课堂上使用的方法来设置一周的时间表
public static String getSunday() {
SimpleDateFormat d = new SimpleDateFormat(dayDate);
Calendar specific = cal;
specific.add(Calendar.DAY_OF_YEAR, (cal.getFirstDayOfWeek() - ??));
return d.format(specific.getTime());
}
At the moment, I'm creating a java schedule app and I was wondering how to find the current day of the week while using the Calendar class to get the first day of the week.
Here is the method I'm using in a class in order to set up a week schedule
public static String getSunday() {
SimpleDateFormat d = new SimpleDateFormat(dayDate);
Calendar specific = cal;
specific.add(Calendar.DAY_OF_YEAR, (cal.getFirstDayOfWeek() - ??));
return d.format(specific.getTime());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过使用
Calendar.DAY_OF_WEEK
调用get()
来获取当前星期几另外,虽然我不确定我是否完全理解您想要做什么,我觉得很可疑。 (例如,
cal
是什么?)You can get the current day of the week by calling
get()
withCalendar.DAY_OF_WEEK
Also, while I'm not sure I understand exactly what you're trying to do, it looks fishy to me. (What's
cal
for example?)太长了;博士
详细信息
问题和其他答案都使用过时的课程。
旧的日期时间类设计不佳、令人困惑且麻烦。避开他们。
java.time
在 Java 8 及更高版本中,我们使用内置的 java.time 框架。许多功能都向后移植到 Java 6 和 Java 6。 7(ThreeTen-Backport),并进一步适配Android(ThreeTenABP)。
即时
表示 UTC 中的当前时刻,分辨率高达纳秒。要获取星期几,我们必须确定日期。时区对于确定日期至关重要,因为在任何特定时刻,全球各地的日期都可能有所不同。例如,巴黎午夜过后几分钟就是新的一天,而在蒙特利尔仍然是“昨天”。
使用
ZoneId
代表时区。请求正确的时区名称,格式为大陆/地区.切勿使用主流媒体中常见的 3-4 个字母缩写,因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。
使用方便的
DayOfWeek
< /a> 枚举来确定星期几。您可以询问
DayOfWeek
对象。您可以索取号码 从 1 到 7,其中 1 是星期一,7 是星期日,根据 ISO 8601标准。但不要在代码中传递这个数字;而是传递
DayOfWeek
对象来享受类型安全和保证有效值的好处。您可能需要文本、星期几的名称。让java.time自动将名字翻译成人类语言。
区域设置
人类语言的种类,并指定使用的文化规范,例如如何缩写和大写。TextStyle
< /a> enum 表示您想要的名称的长度。要开始一周,请询问上一个星期一(或星期日,等等),或者如果是星期一则坚持今天。使用
TemporalAdjusters
类中找到的TemporalAdjuster
实现。如果您需要日期时间而不是仅日期,请要求 java.time 获取一天中的第一个时刻(并不总是时间
00:00:00
)。关于 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
Details
The Question and other Answer both use outmoded classes.
The old date-time classes are poorly designed, confusing, and troublesome. Avoid them.
java.time
In Java 8 and later, we use the built-in java.time framework. Much of the functionality is back-ported to Java 6 & 7 (ThreeTen-Backport), and further adapted to Android (ThreeTenABP).
An
Instant
represents the current moment in UTC with a resolution up to nanoseconds.To get the day-of-week we must determine the date. Time zone is crucial in determining the date as the date can vary around the globe for any given moment. For example, a few minutes after midnight in Paris is a new day, while in Montréal it is still “yesterday”.
Use
ZoneId
to represent a time zone. Ask for a proper time zone name in format ofcontinent/region
. Never use the 3-4 letter abbreviations commonly seen in mainstream media as they are not true time zones, not standardized, and are not even unique(!).Use the handy
DayOfWeek
enum to determine the day of the week.You may interrogate the
DayOfWeek
object.You may ask for a number from 1 to 7 where 1 is Monday and 7 is Sunday per the ISO 8601 standard. But do not pass this number around your code; instead pass around
DayOfWeek
objects to enjoy the benefits of type-safety and guaranteed valid values.You may want text, the name of the day of the week. Let java.time automatically translate the name into a human language. A
Locale
species which human language, and also specifies the cultural norms to use such as how to abbreviate and capitalize. TheTextStyle
enum indicates how long a name you want.To get the start of the week, ask for the previous Monday (or Sunday, whatever), or stick with today if it is a Monday. Use a
TemporalAdjuster
implementation found inTemporalAdjusters
class.If you need a date-time rather than date-only, ask java.time to get the first moment of the day (not always the time
00:00:00
).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.