Mark F
Mark F

Reputation: 1543

Gregorian Calendar Date Confusion

I'm confused about how a Gregorian Calendar associates days with numbers. For examples, I instantiate an object

GregorianCalendar cal = new GregorianCalendar();
Toast.makeText(context, "Day: " + cal.DAY_OF_WEEK, Toast.LENGTH_LONG).show();

My toast messages keeps dispalying "Day: 7"

Today is Friday, assuming that Sunday = 0, shouldn't the text display "Day: 5"?

It does work when I do the following:

int current day = cal.get(Calendar.DAY_OF_WEEK)

Can someone explain why? Thank you.

Upvotes: 1

Views: 59

Answers (1)

scottt
scottt

Reputation: 8381

The reason that you're getting a 7 with cal.DAY_OF_WEEK, is that you're actually asking for the value of the constant named DAY_OF_WEEK and the value of that field is 7. See here. In other words, cal.DAY_OF_WEEK is really equivalent to Calendar.DAY_OF_WEEK.

You get the correct answer with cal.get(Calendar.DAY_OF_WEEK), because you're then asking for the value of cal's DAY_OF_WEEK field.

Upvotes: 1

Related Questions