Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsure what get(Calendar.DAY_OF_WEEK) returns

My problem can be easily created by the scenario below:

 //create a gregorian calendar object that set the date and time as 4th June 2012 at 10:30PM
 Calendar calendar = new GregorianCalendar(2012, 6, 4, 22, 30);

 //when I print out these:
 System.out.println(calendar.get(Calendar.DAY_OF_WEEK));
 System.out.println(calendar.get(Calendar.MINUTE));
 System.out.println(calendar.get(Calendar.HOUR));
 System.out.println(calendar.get(Calendar.DATE));
 System.out.println(calendar.get(Calendar.MONTH));
 System.out.println(calendar.get(Calendar.YEAR));

 //output reads as:
 4
 30
 10
 4
 6
 2012

 //so does calendar.get(Calendar.DAY_OF_WEEK) == calendar.get(Calendar.DATE) ???

Just so that everyone is clear the 4th of June 2012 is a Monday, so shouldn't calendar.get(Calendar.DAY_OF_WEEK) return 0 as part of the first day of the week?

Thank for your all your help and concerns, please also verify the source that you are referring to.

user1442080

like image 628
user1442080 Avatar asked Sep 11 '25 05:09

user1442080


1 Answers

The month in java Calendar classes is 0-based. So June is month number 5.

You actually created an object representing July 4th, which happens to be a Wednesday, which is the fourth day of that week.

like image 176
Kees de Kooter Avatar answered Sep 13 '25 23:09

Kees de Kooter