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);
Run Code Online (Sandbox Code Playgroud)
Mah*_*aha 19
Calendar.WEEK_OF_MONTH只返回"当月的当前周数"Calendar.DAY_OF_WEEK只返回"从上周日开始的当前周的当前日期" Calendar.DAY_OF_WEEK_IN_MONTH如果当天是月的第N天则返回"N"说"如果今天是3月的第3个星期三那么"所以我在2016年12月21日写这篇文章:
这就是我得到的:
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
Run Code Online (Sandbox Code Playgroud)
小智 17
区别在于DAY_OF_WEEK_IN_MONTH提供了工作日在该月份发生的次数,WEEK_OF_MONTH只返回当月内的周数.可以这样想,如果月份从星期三开始,则第一个星期一将在该月的第二周开始.该周一的DAY_OF_WEEK_IN_MONTH值为1,但WEEK_OF_MONTH为2.
我发现所有其他文档都令人困惑,所以对于像我这样的微软开发人员来说,这对你来说可能很清楚,就像我一样:
http://msdn.microsoft.com/en-us/library/aa986432(v=vs.80).aspx
一个常量,表示该月中某一天发生了多少次的值.
Tre*_*e14 -1
每月的周数是当月内的第几周,从星期日开始已经有多少周了。
一个月中的第 5 天是星期四、第 1 个星期日等。