Reputation: 159
I am a 3 month old java student. For one of my module I tried to make first day of week as tuesday(so that a real friday should now be at index 3) but it didn't show expected result.
I am inputting day through scanner. Below is the concerned piece of code and output:
Calendar c= Calendar.getInstance();
c.setFirstDayOfWeek(Calendar.TUESDAY);
c.setTime(date);
int dayOfWeek=c.get(Calendar.DAY_OF_WEEK);
System.out.println(dayOfWeek);
Output:
Enter the date(dd/mm/yyyy):
03/07/2014
6
Don't know where I am wrong. Tried a lot googling around and even set minimalDAysOfFirstWeek but nothing working out. What i want is 03/07/2014 to be shown as index 3. How to achieve so?
Upvotes: 1
Views: 176
Reputation: 2275
switch (c.get(Calendar.DAY_OF_WEEK)) {
case Calendar.TUESDAY:
return 0;
case Calendar.WEDNESDAY:
return 1;
// And so on...
default:
break;
}
Upvotes: 0
Reputation: 1499800
I think you've misunderstood the purpose of setFirstDayOfWeek
.
That doesn't change c.get(Calendar.DAY_OF_WEEK)
works at all - it changes the result of calling c.get(Calendar.WEEK_OF_MONTH)
and c.get(Calendar.WEEK_OF_YEAR)
, as per the documentation:
When setting or getting the
WEEK_OF_MONTH
orWEEK_OF_YEAR
fields, Calendar must determine the first week of the month or year as a reference point. The first week of a month or year is defined as the earliest seven day period beginning ongetFirstDayOfWeek()
and containing at leastgetMinimalDaysInFirstWeek()
days of that month or year.
In other words, Sunday is always Sunday... but whether Sunday June 10th is in the same week as Monday June 11th or not depends on what is considered to be the first day of the week.
Upvotes: 6