Java 的 Calendar 类中的 Calendar.WEEK_OF_MONTH 和 Calendar.DAY_OF_WEEK_IN_MONTH 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6538791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:16:15  来源:igfitidea点击:

What is the difference between Calendar.WEEK_OF_MONTH and Calendar.DAY_OF_WEEK_IN_MONTH in Java's Calendar class?

javacalendar

提问by Mandel

Java's Calendar class provides for two fields: WEEK_OF_MONTH and DAY_OF_WEEK_IN_MONTH. Can someone explain the difference to me? It seems that they both return the same value when tested using the code below:

Java 的 Calendar 类提供了两个字段:WEEK_OF_MONTH 和 DAY_OF_WEEK_IN_MONTH。有人可以向我解释其中的区别吗?使用以下代码进行测试时,它们似乎都返回相同的值:

Calendar date = Calendar.getInstance();
date.set(2011,5,29);
int weekNo1 = date.get(Calendar.WEEK_OF_MONTH);
int weekNo2 = date.get(Calendar.DAY_OF_WEEK_IN_MONTH);

采纳答案by Treesrule14

Week of Month is the week within the current month starting from sundays how many weeks have there been.

Week of Month 是从星期日开始的当月中的一周,有多少周。

Day of week of month is the day 5 would be Thursday, 1 sunday ect.

一个月中的第 5 天是星期四,第 1 个星期日等。

回答by Mahesha999

  • Calendar.WEEK_OF_MONTHsimply returns "Current week number in current month"
  • Calendar.DAY_OF_WEEKsimply returns "Current day number in current week starting on last Sunday"
  • Calendar.DAY_OF_WEEK_IN_MONTHreturns "N if current day is Nth day of month"say "3 if today is 3rd Wednesday in month"
  • Calendar.WEEK_OF_MONTH简单地返回“当前月份的当前周数”
  • Calendar.DAY_OF_WEEK简单地返回“从上周日开始的当前周中的当前天数”
  • Calendar.DAY_OF_WEEK_IN_MONTH返回“N 如果当前日期是一个月的第 N 天”“3 如果今天是一个月中的第三个星期三”

So I am writing this on 21st December 2016:

所以我在 2016 年 12 月 21 日写了这篇文章:

enter image description here

在此处输入图片说明

And this is what I am getting:

这就是我得到的:

Calendar today = Calendar.getInstance();
System.out.println(today.get(Calendar.DAY_OF_WEEK));          //outputs 4, as today is 4th day in this week which started on 18
System.out.println(today.get(Calendar.DAY_OF_WEEK_IN_MONTH)); //outputs 3, as today is "3rd Wednesday of this month". Earlier two wednesday were on 7th and 14th
System.out.println(today.get(Calendar.WEEK_OF_MONTH));        //outputs 4, as currently 4th week of a month is running

回答by user3096406

The difference is that DAY_OF_WEEK_IN_MONTH provides the number of times the weekday has occurred during the month and WEEK_OF_MONTH just returns the week number within the current month. Think of it this way, if the month starts on a Wednesday, the first Monday will occur during the second week of the month. The value for DAY_OF_WEEK_IN_MONTH for that Monday would be 1, but the WEEK_OF_MONTH would be 2.

区别在于 DAY_OF_WEEK_IN_MONTH 提供了该月中工作日发生的次数,而 WEEK_OF_MONTH 仅返回当月内的周数。这样想,如果一个月从星期三开始,第一个星期一将发生在该月的第二周。该星期一的 DAY_OF_WEEK_IN_MONTH 的值为 1,但 WEEK_OF_MONTH 的值为 2。

回答by eselk

I found all of the other docs confusing, so for any Microsoft developers like myself this one might be clear for you, as it was for me:

我发现所有其他文档都令人困惑,因此对于像我这样的任何 Microsoft 开发人员来说,这对您来说可能很清楚,就像对我一样:

http://msdn.microsoft.com/en-us/library/aa986432(v=vs.80).aspx

http://msdn.microsoft.com/en-us/library/aa986432(v=vs.80).aspx

A constant representing a value for how many times a given day has occurred in the month.

一个常数,表示一个月中给定日期发生了多少次的值。

回答by Luis Ivan Mera Dávila

You must also take in count that Calendar.getInstance depends on Locale. So, you sometimes could have to specify a concrete Locale instead of the default Locale, for instance: Calendar.getInstance(new Locale("es","PE").

您还必须考虑 Calendar.getInstance 取决于 Locale。因此,有时您可能必须指定具体的 Locale 而不是默认的 Locale,例如:Calendar.getInstance(new Locale("es","PE")。

For the example, avobe, Calendar.getInstance(new Locale("es","PE") the calendar will consider first days of the week the Mondays, in other Locales could be other days

例如, avobe, Calendar.getInstance(new Locale("es","PE") 日历将考虑一周的第一天星期一,在其他语言环境中可能是其他天

回答by Basil Bourque

tl;dr

tl;博士

  • Calendar.WEEK_OF_MONTH
    • Obsolete, never use Calendar.
    • Means the nth week within a month, with definition varying by Localeand the result of getFirstDayOfWeek().
    • If you want to define a week as week-of-month # 1 is the week containing day-of-month 1, then use: LocalDate::get( ChronoField.ALIGNED_WEEK_OF_MONTH )
  • Calendar.DAY_OF_WEEK_IN_MONTH
    • Obsolete, never use Calendar.
    • Means the nth day-of-week within that month, such 2nd Tuesday, where weeks are defined as week # 1 containing day 1 of that month.
    • Replaced by LocalDate::get( ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH )
  • Calendar.WEEK_OF_MONTH
    • 已过时,切勿使用Calendar.
    • 表示一个月内的第 n 周,定义因Locale和结果而异getFirstDayOfWeek()
    • 如果您想将一周定义为 week-of-month #1 是包含 day-of-month 1 的周,则使用: LocalDate::get( ChronoField.ALIGNED_WEEK_OF_MONTH )
  • Calendar.DAY_OF_WEEK_IN_MONTH
    • 已过时,切勿使用Calendar.
    • 表示该月内的第 n 个星期,例如第 2 个星期二,其中周被定义为包含该月第 1 天的第 1 周。
    • 取而代之 LocalDate::get( ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH )

Avoid Calendarlegacy class

避免Calendar遗留类

Be aware that the Calendarclass used in the question is terrible, and was supplanted years ago by the modern java.timeclasses defined in JSR 310. Do not use Calendarnor GregorianCalendar. They were designed by people who did not understand date-time handling.

请注意,Calendar问题中使用的类很糟糕,并且在几年前被JSR 310 中定义的现代java.time类取代。不要使用或。它们是由不了解日期时间处理的人设计的。CalendarGregorianCalendar

java.time

时间

LocalDate

LocalDate

The LocalDateclass represents a date-only value without time-of-day and without time zoneor offset-from-UTC.

LocalDate级表示没有时间的天,没有一个日期,只值时区偏移从-UTC

LocalDate ld = LocalDate.of( 2020 , Month.JANUARY , 23 ) ;  // 2020-01-23.

Convert

兑换

If given a Calendarobject that is really a GregorianCalendar, you can easily convert to a modern ZonedDateTimeobject.

如果给定一个Calendar真正是a 的对象GregorianCalendar,您可以轻松转换为现代ZonedDateTime对象。

GregorianCalendar gc = ( GregorianCalendar ) myCalendar ;  // Cast from more general class to the concrete class underlying.
ZonedDateTime zdt = gc.toZonedDateTime() ;

Extract the date-only portion.

提取仅日期部分。

LocalDate ld = zdt.toLocalDate() ;  // Extract the date, omitting the time-of-day and the context of a time zone.

ChronoField

ChronoField

You can interrogate a LocalDateobject via the getmethod with various ChronoFieldenum objects.

您可以LocalDate通过get具有各种ChronoField枚举对象的方法来查询对象。

Calendar.WEEK_OF_MONTH

Calendar.WEEK_OF_MONTH

This asks for the week of the month. But how do you define a week of the month? Unfortunately, the Calendarclass' definition varies by Locale. So your results may vary at runtime.

这要求一个月中的第几周。但是你如何定义一个月中的一周?不幸的是,Calendar类的定义因Locale. 因此,您的结果可能会在运行时有所不同。

If your definition is that week # 1 has the first day of the calendar month, you can interrogate a LocalDateusing ChronoField.ALIGNED_WEEK_OF_MONTH.

如果您的定义是第 1 周是日历月的第一天,您可以LocalDate使用ChronoField.ALIGNED_WEEK_OF_MONTH.

int weekOfMonth = ld.get( ChronoField.ALIGNED_WEEK_OF_MONTH ) ;

Calendar.DAY_OF_WEEK_IN_MONTH

Calendar.DAY_OF_WEEK_IN_MONTH

This means the nth day-of-week found in the month, such as 2nd-Tuesday or third-Thursday. Unlike Calendar.WEEK_OF_MONTH, this definition does notvary by Locale. Week # 1 is the week containing day-of-month 1. This legacy Calendarclass really is an awful mess, confusing and inconsistent. Good riddance.

这意味着在该月中找到的第 n 个星期几,例如第二个星期二或第三个星期四。不像Calendar.WEEK_OF_MONTH,这个定义并没有被改变Locale。第 1 周是包含第 1 天的周。这个遗留Calendar类真的是一团糟,令人困惑和不一致。甩掉包袱。

Now in java.time, use instead ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH.

现在在java.time 中,改用ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH.

DayOfWeek dow = ld.getDayOfMonth() ;  // Get enum object representing day of week such as `DayOfWeek.MONDAY`. 
int nthDayOfWeekOfMonth = ld.get( ChronoField.ALIGNED_DAY_OF_WEEK_IN_MONTH ) ;