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

  • 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"

So I am writing this on 21st December 2016:

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

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.


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:

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.

Tags:

Java

Calendar