Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
710 views
in Technique[技术] by (71.8m points)

date - java get day of week is not accurate

I am trying to determine what day of the week is the first day of the month but for some reason it is not returning me the correct day of the week.

Here is my code below:

CalendarMonth[] months = CalendarUtils.constructMonthViewArray(new GregorianCalendar());


    public static CalendarMonth[] constructMonthViewArray(Calendar cal) {
        CalendarMonth[] months = new CalendarMonth[CALENDAR_GRID_SIZE];


        int year = cal.get(cal.YEAR);
        int month = cal.get(cal.MONTH);;
        // calculate how many days in the month
        int numOfDays = getNumOfDaysInMonth(cal);
        // calculate what day(mon-sunday) is the 1st of the month
        int firstDayOfMonth = getFirstDayOfMonth(cal);



private static int getFirstDayOfMonth(Calendar cal) {
        int firstDay = cal.get(Calendar.DAY_OF_WEEK);

        Log.d(TAG, "");

        // decrement it because our array deals with values 0-6(indexes)
        firstDay--;


        if (firstDay == 0) {
            firstDay = 6;
        } else {
            // decrement again so that the days start from 0.
            firstDay--;
        }
        return firstDay;
    }

The line from "int firstDay = cal.get(Calendar.DAY_OF_WEEK);" fails to give me the correct day of the week and returns the value 2 for getting the 1st day of this month(January 2011) when the first of the month was on a Saturday(7).

Am I missing something? I have debugged and checked what month, year and date the cal variable is set and it indeed indicated today's date as corrected but when i get the day of week it doesn't get the value 7.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I can't reproduce the problem you're seeing. As Michael says, there's a lot of code you haven't shown us, but Calendar.getDayOfWeek definitely works:

import java.util.*;

public class Test {
    public static void main(String[] args) {
        Calendar calendar = new GregorianCalendar();
        calendar.set(2011, 0, 1); // 0 = January
        System.out.println(calendar.get(Calendar.DAY_OF_WEEK)); // Prints 7
    }
}

Did you maybe forget that months are 0-based in java.util.Calendar?

If you can produce a similar short but complete program which shows the wrong day of the week, please post it.

The fact that you're decrementing firstDay twice within getFirstDayOfMonth seems somewhat odd, as well as the fact that it doesn't really reflect the name of the method (as Michael mentioned).

Finally, my constant recommendation for Java date/time handling: if you can possibly use Joda Time instead of java.util.Calendar, do so. It's a much, much better API.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...