Java:获取当前星期几的值

发布于 2024-08-24 23:20:22 字数 362 浏览 6 评论 0原文

目前,我正在创建一个 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 技术交流群。

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

发布评论

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

评论(2

几味少女 2024-08-31 23:20:22

您可以通过使用 Calendar.DAY_OF_WEEK 调用 get() 来获取当前星期几

public int getTodaysDayOfWeek() {
  final Calendar c = Calendar.getInstance();
  return c.get(Calendar.DAY_OF_WEEK);
}

另外,虽然我不确定我是否完全理解您想要做什么,我觉得很可疑。 (例如,cal 是什么?)

You can get the current day of the week by calling get() with Calendar.DAY_OF_WEEK

public int getTodaysDayOfWeek() {
  final Calendar c = Calendar.getInstance();
  return c.get(Calendar.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?)

别理我 2024-08-31 23:20:22

太长了;博士

如何查找当前星期几

DayOfWeek.from( LocalDate.now() )

制定一周计划

LocalDate sunday = 
    LocalDate.now()
             .with( TemporalAdjusters.nextOrSame( DayOfWeek.SUNDAY ) ) ;

LocalDate monday = sunday.plusDays( 1 ) ;
LocalDate tuesday = monday.plusDays( 1 ) ;
…

详细信息

问题和其他答案都使用过时的课程。

旧的日期时间类设计不佳、令人困惑且麻烦。避开他们。

java.time

在 Java 8 及更高版本中,我们使用内置的 java.time 框架。许多功能都向后移植到 Java 6 和 Java 6。 7(ThreeTen-Backport),并进一步适配Android(ThreeTenABP)。

即时 表示 UTC 中的当前时刻,分辨率高达纳秒。

Instant instant = Instant.now();

要获取星期几,我们必须确定日期。时区对于确定日期至关重要,因为在任何特定时刻,全球各地的日期都可能有所不同。例如,巴黎午夜过后几分钟就是新的一天,而在蒙特利尔仍然是“昨天”。

使用 ZoneId 代表时区。请求正确的时区名称,格式为大陆/地区.切勿使用主流媒体中常见的 3-4 个字母缩写,因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。

ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId );

使用方便的 DayOfWeek< /a> 枚举来确定星期几。

DayOfWeek dow = DayOfWeek.from( zdt );

您可以询问 DayOfWeek 对象。

您可以索取号码 从 1 到 7,其中 1 是星期一,7 是星期日,根据 ISO 8601标准。但不要在代码中传递这个数字;而是传递 DayOfWeek 对象来享受类型安全和保证有效值的好处。

int dowNumber = dow.getValue();

您可能需要文本、星期几的名称。让java.time自动将名字翻译成人类语言。 区域设置 人类语言的种类,并指定使用的文化规范,例如如何缩写和大写。 TextStyle< /a> enum 表示您想要的名称的长度。

Locale locale = Locale.CANADA_FRENCH;
String output = dow.getDisplayName( TextStyle.FULL , locale );

要开始一周,请询问上一个星期一(或星期日,等等),或者如果是星期一则坚持今天。使用 TemporalAdjusters 类中找到的 TemporalAdjuster 实现。

LocalDate startOfWeek = zdt.toLocalDate().with( TemporalAdjusters.previousOrSame( DayOfWeek.MONDAY ) );

如果您需要日期时间而不是仅日期,请要求 java.time 获取一天中的第一个时刻(并不总是时间 00:00:00)。

ZonedDateTime zdtWeekStart = startOfWeek.atStartOfDay( zoneId );

关于 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

how to find the current day of the week

DayOfWeek.from( LocalDate.now() )

set up a week schedule

LocalDate sunday = 
    LocalDate.now()
             .with( TemporalAdjusters.nextOrSame( DayOfWeek.SUNDAY ) ) ;

LocalDate monday = sunday.plusDays( 1 ) ;
LocalDate tuesday = monday.plusDays( 1 ) ;
…

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.

Instant instant = Instant.now();

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 of continent/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(!).

ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId );

Use the handy DayOfWeek enum to determine the day of the week.

DayOfWeek dow = DayOfWeek.from( zdt );

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.

int dowNumber = dow.getValue();

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. The TextStyle enum indicates how long a name you want.

Locale locale = Locale.CANADA_FRENCH;
String output = dow.getDisplayName( TextStyle.FULL , locale );

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 in TemporalAdjusters class.

LocalDate startOfWeek = zdt.toLocalDate().with( TemporalAdjusters.previousOrSame( DayOfWeek.MONDAY ) );

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

ZonedDateTime zdtWeekStart = startOfWeek.atStartOfDay( zoneId );

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.

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