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
What is the difference between Calendar.WEEK_OF_MONTH and Calendar.DAY_OF_WEEK_IN_MONTH in Java's Calendar class?
提问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_MONTH
simply returns "Current week number in current month"Calendar.DAY_OF_WEEK
simply returns "Current day number in current week starting on last Sunday"Calendar.DAY_OF_WEEK_IN_MONTH
returns "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 日写了这篇文章:
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
Locale
and the result ofgetFirstDayOfWeek()
. - 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 )
- Obsolete, never use
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 )
- Obsolete, never use
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 Calendar
legacy class
避免Calendar
遗留类
Be aware that the Calendar
class used in the question is terrible, and was supplanted years ago by the modern java.timeclasses defined in JSR 310. Do not use Calendar
nor GregorianCalendar
. They were designed by people who did not understand date-time handling.
请注意,Calendar
问题中使用的类很糟糕,并且在几年前被JSR 310 中定义的现代java.time类取代。不要使用或。它们是由不了解日期时间处理的人设计的。Calendar
GregorianCalendar
java.time
时间
LocalDate
LocalDate
The LocalDate
class 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 Calendar
object that is really a GregorianCalendar
, you can easily convert to a modern ZonedDateTime
object.
如果给定一个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 LocalDate
object via the get
method with various ChronoField
enum objects.
您可以LocalDate
通过get
具有各种ChronoField
枚举对象的方法来查询对象。
This asks for the week of the month. But how do you define a week of the month? Unfortunately, the Calendar
class' 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 LocalDate
using ChronoField.ALIGNED_WEEK_OF_MONTH
.
如果您的定义是第 1 周是日历月的第一天,您可以LocalDate
使用ChronoField.ALIGNED_WEEK_OF_MONTH
.
int weekOfMonth = ld.get( ChronoField.ALIGNED_WEEK_OF_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 Calendar
class 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 ) ;