get Month Offset as int value - Android java.util

Android examples for java.util:Month

Description

get Month Offset as int value

Demo Code


//package com.java2s;

import java.util.Calendar;

public class Main {
    /**/*from   ww  w  . j  a va  2  s.  c om*/
     *
     * @param currentCalendar
     * @param firstDayOfWeek
     * @return
     */
    public static int getMonthOffset(Calendar currentCalendar,
            int firstDayOfWeek) {
        final Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(firstDayOfWeek);
        calendar.setTime(currentCalendar.getTime());
        calendar.set(Calendar.DAY_OF_MONTH, 1);

        int firstDayWeekPosition = calendar.getFirstDayOfWeek();
        int dayPosition = calendar.get(Calendar.DAY_OF_WEEK);

        if (firstDayWeekPosition == 1) {
            return dayPosition - 1;
        } else {
            if (dayPosition == 1) {
                return 6;
            } else {
                return dayPosition - 2;
            }
        }
    }
}

Related Tutorials