Get day (weeks) of a Java special date?

I have a Gregorian date and I want to get the day of the week. I tried this but got the wrong answer:

Calendar calendar = new GregorianCalendar();

calendar.set(myYear, myMonth, myDay);

int result = calendar.get(Calendar.DAY_OF_WEEK);
switch (result) {
case Calendar.SUNDAY:
    Log.i("DayOfWeek", "SUN");
    break;

.
.
.

default:
    startDay = 0;
    break;
} 

      

What is the problem?

+3


source to share


1 answer


I finally found the problem! The above code is correct, but you must pass the standard month value (0 ... 11) instead of (1 ... 12):



Calendar calendar = new GregorianCalendar();

calendar.set(myYear, myMonth-1, myDay);

int result = calendar.get(Calendar.DAY_OF_WEEK);
switch (result) {
case Calendar.SUNDAY:
    Log.i("DayOfWeek", "SUN");
    break;

.
.
.

default:
    startDay = 0;
    break;
}

      

+1


source







All Articles