Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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);
like image 230
Mandel Avatar asked Jun 30 '11 17:06

Mandel


People also ask

What is Calendar Day_of_week in Java?

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).

What is the calendar class in Java?

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.

What does calendar date return in Java?

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.


1 Answers

  • 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
like image 62
Mahesha999 Avatar answered Sep 17 '22 22:09

Mahesha999