adjust Calendar By Add Month - Java java.util

Java examples for java.util:Calendar Calculation

Description

adjust Calendar By Add Month

Demo Code


//package com.java2s;
import java.util.Calendar;

public class Main {
    static void adjustCalendarByAddMonth(Calendar calendar, int offsetMonth) {
        int oriMonth = calendar.get(Calendar.MONTH);
        int totalMonth = oriMonth + offsetMonth;
        int offsetYear = totalMonth / 12;

        int destMonth = totalMonth % 12;
        if (destMonth < 0) {
            destMonth += 12;/*from w  ww .j  a va 2  s  .  c  o m*/
            offsetYear -= 1;
        }

        int oriYear = calendar.get(Calendar.YEAR);
        calendar.set(Calendar.YEAR, oriYear + offsetYear);
        calendar.set(Calendar.MONTH, destMonth);
    }
}

Related Tutorials