FileDocCategorySizeDatePackage
MonthDisplayHelper.javaAPI DocAndroid 1.5 API5595Wed May 06 22:41:56 BST 2009android.util

MonthDisplayHelper

public class MonthDisplayHelper extends Object
Helps answer common questions that come up when displaying a month in a 6 row calendar grid format. Not thread safe.

Fields Summary
private final int
mWeekStartDay
private Calendar
mCalendar
private int
mNumDaysInMonth
private int
mNumDaysInPrevMonth
private int
mOffset
Constructors Summary
public MonthDisplayHelper(int year, int month, int weekStartDay)

param
year The year.
param
month The month.
param
weekStartDay What day of the week the week should start.


        if (weekStartDay < Calendar.SUNDAY || weekStartDay > Calendar.SATURDAY) {
            throw new IllegalArgumentException();
        }
        mWeekStartDay = weekStartDay;

        mCalendar = Calendar.getInstance();
        mCalendar.set(Calendar.YEAR, year);
        mCalendar.set(Calendar.MONTH, month);
        mCalendar.set(Calendar.DAY_OF_MONTH, 1);
        mCalendar.set(Calendar.HOUR_OF_DAY, 0);
        mCalendar.set(Calendar.MINUTE, 0);
        mCalendar.set(Calendar.SECOND, 0);
        mCalendar.getTimeInMillis();

        recalculate();
    
public MonthDisplayHelper(int year, int month)

        this(year, month, Calendar.SUNDAY);
    
Methods Summary
public intgetColumnOf(int day)

return
Which column day is in.

        return (day + mOffset - 1) % 7;
    
public intgetDayAt(int row, int column)

param
row The row, 0-5, starting from the top.
param
column The column, 0-6, starting from the left.
return
The day at a particular row, column


        if (row == 0 && column < mOffset) {
            return mNumDaysInPrevMonth + column - mOffset + 1;
        }

        int day = 7 * row + column - mOffset + 1;

        return (day > mNumDaysInMonth) ?
                day - mNumDaysInMonth : day;
    
public int[]getDigitsForRow(int row)

param
row Which row (0-5).
return
the digits of the month to display in one of the 6 rows of a calendar month display.

        if (row < 0 || row > 5) {
            throw new IllegalArgumentException("row " + row
                    + " out of range (0-5)");
        }

        int [] result = new int[7];
        for (int column = 0; column < 7; column++) {
            result[column] = getDayAt(row, column);
        }

        return result;
    
public intgetFirstDayOfMonth()

return
The first day of the month using a constants such as {@link java.util.Calendar#SUNDAY}.

        return mCalendar.get(Calendar.DAY_OF_WEEK);
    
public intgetMonth()

        return mCalendar.get(Calendar.MONTH);
    
public intgetNumberOfDaysInMonth()

return
The number of days in the month.

        return mNumDaysInMonth;
    
public intgetOffset()

return
The offset from displaying everything starting on the very first box. For example, if the calendar is set to display the first day of the week as Sunday, and the month starts on a Wednesday, the offset is 3.

        return mOffset;
    
public intgetRowOf(int day)

return
Which row day is in.

        return (day + mOffset - 1) / 7;
    
public intgetWeekStartDay()

        return mWeekStartDay;
    
public intgetYear()

        return mCalendar.get(Calendar.YEAR);
    
public booleanisWithinCurrentMonth(int row, int column)

return
Whether the row and column fall within the month.


        if (row < 0 || column < 0 || row > 5 || column > 6) {
            return false;
        }

        if (row == 0 && column < mOffset) {
            return false;
        }

        int day = 7 * row + column - mOffset + 1;
        if (day > mNumDaysInMonth) {
            return false;
        }
        return true;
    
public voidnextMonth()
Increment the month.

        mCalendar.add(Calendar.MONTH, 1);
        recalculate();
    
public voidpreviousMonth()
Decrement the month.

        mCalendar.add(Calendar.MONTH, -1);
        recalculate();
    
private voidrecalculate()


        mNumDaysInMonth = mCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);

        mCalendar.add(Calendar.MONTH, -1);
        mNumDaysInPrevMonth = mCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        mCalendar.add(Calendar.MONTH, 1);

        int firstDayOfMonth = getFirstDayOfMonth();
        int offset = firstDayOfMonth - mWeekStartDay;
        if (offset < 0) {
            offset += 7;
        }
        mOffset = offset;