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:
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);
A day-of-week, such as 'Tuesday'. DayOfWeek is an enum representing the 7 days of the week - Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday. In addition to the textual enum name, each day-of-week has an int value. The int value follows the ISO-8601 standard, from 1 (Monday) to 7 (Sunday).
The Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR , MONTH , DAY_OF_MONTH , HOUR , and so on, and for manipulating the calendar fields, such as getting the date of the next week.
It is used to return the value of the given calendar field. It is used to return the maximum value for the given calendar field of this Calendar instance.
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With