Java Calendar Add addMonthDayToCal(Calendar cal, int month, int day)

Here you can find the source of addMonthDayToCal(Calendar cal, int month, int day)

Description

add Month Day To Cal

License

Open Source License

Declaration

@Deprecated
    public static Calendar addMonthDayToCal(Calendar cal, int month, int day) 

Method Source Code

//package com.java2s;
/*//from   w ww .j a  va 2  s . c  o  m
 * @ (#) CalendarUtils.java
 * 
 * Copyright (c) 2010 ClickDiagnostics Inc. All Rights Reserved. This software is the
 * confidential and proprietary information of ClickDiagnostics ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with ClickDiagnostics.
 */

import java.util.Calendar;
import java.util.Date;

public class Main {
    public final static long MILLIS_PER_DAY = 24 * 60 * 60 * 1000L;

    @Deprecated
    public static Calendar addMonthDayToCal(Calendar cal, int month, int day) {
        Date d = new Date();
        int daysToAdd = 0;
        // Calendar cal = Calendar.getInstance();
        int currentMonth = cal.get(2);
        long t = 0;

        t = d.getTime();
        if (month > 0) {
            // cal.get ( month );
            for (int i = 0; i < month; i++) {
                if (currentMonth < 12) {
                    if (currentMonth == Calendar.JANUARY) {
                        cal.set(2, Calendar.JANUARY);
                        daysToAdd += cal.getActualMaximum(Calendar.DAY_OF_MONTH);
                    } else if (currentMonth == Calendar.FEBRUARY) {
                        cal.set(2, Calendar.FEBRUARY);
                        daysToAdd += cal.getActualMaximum(Calendar.DAY_OF_MONTH);
                    } else if (currentMonth == Calendar.MARCH) {
                        cal.set(2, Calendar.MARCH);
                        daysToAdd += cal.getActualMaximum(Calendar.DAY_OF_MONTH);
                    } else if (currentMonth == Calendar.APRIL) {
                        cal.set(2, Calendar.APRIL);
                        daysToAdd += cal.getActualMaximum(Calendar.DAY_OF_MONTH);
                    } else if (currentMonth == Calendar.MAY) {
                        cal.set(2, Calendar.MAY);
                        daysToAdd += cal.getActualMaximum(Calendar.DAY_OF_MONTH);
                    } else if (currentMonth == Calendar.JUNE) {
                        cal.set(2, Calendar.JUNE);
                        daysToAdd += cal.getActualMaximum(Calendar.DAY_OF_MONTH);
                    } else if (currentMonth == Calendar.JULY) {
                        cal.set(2, Calendar.JULY);
                        daysToAdd += cal.getActualMaximum(Calendar.DAY_OF_MONTH);
                    } else if (currentMonth == Calendar.AUGUST) {
                        cal.set(2, Calendar.AUGUST);
                        daysToAdd += cal.getActualMaximum(Calendar.DAY_OF_MONTH);
                    } else if (currentMonth == Calendar.SEPTEMBER) {
                        cal.set(2, Calendar.SEPTEMBER);
                        daysToAdd += cal.getActualMaximum(Calendar.DAY_OF_MONTH);
                    } else if (currentMonth == Calendar.OCTOBER) {
                        cal.set(2, Calendar.OCTOBER);
                        daysToAdd += cal.getActualMaximum(Calendar.DAY_OF_MONTH);
                    } else if (currentMonth == Calendar.NOVEMBER) {
                        cal.set(2, Calendar.NOVEMBER);
                        daysToAdd += cal.getActualMaximum(Calendar.DAY_OF_MONTH);
                    } else if (currentMonth == Calendar.DECEMBER) {
                        cal.set(2, Calendar.DECEMBER);
                        daysToAdd += cal.getActualMaximum(Calendar.DAY_OF_MONTH);
                    }
                    currentMonth++;
                } else {
                    currentMonth = 0;
                    i--;
                }
            }
        }
        daysToAdd += day;
        // add the desired number of days to the long value using milli seconds

        t += daysToAdd * MILLIS_PER_DAY;
        cal.setTime(new Date(t));
        // and now you can format the date the way you want
        String result = Integer.toString(cal.get(Calendar.DAY_OF_MONTH)) + "-"
                + Integer.toString(cal.get(Calendar.MONTH) + 1) + "-" + Integer.toString(cal.get(Calendar.YEAR));
        System.out.println("result month add::" + result);
        return cal;
    }
}

Related

  1. addDays(final int days, final Calendar from)
  2. addDayToCalendar(Calendar calendar, int day)
  3. addLocaleToCalendar(Calendar date, String locale)
  4. addMilliseconds(Calendar calendar, Long amount)
  5. addMonth(Calendar cal, int period)
  6. addSeconds(Calendar calendar, int amount)
  7. addToUTCMilliSeconds(int calendarTime, int actualValue)
  8. addWeekdays(Calendar cal, int days)
  9. addWorkdays(final Calendar calendar, final int dauer)