Java Calendar Adjust adjustDate(Calendar calendar, int differenceInDay)

Here you can find the source of adjustDate(Calendar calendar, int differenceInDay)

Description

Adjusts the Calendar to several days before or after the current date.

License

Open Source License

Parameter

Parameter Description
calendar the Calendar object to be adjusted.
differenceInDay the difference in days. It accepts both position and negative number.

Return

the calendar after the adjustment. It should always be the same instance as the calendar parameter.

Declaration

public static Calendar adjustDate(Calendar calendar, int differenceInDay) 

Method Source Code


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

public class Main {
    private static final long DAY_IN_MS = 24 * 60 * 60 * 1000;

    /**// ww w.  ja v a 2 s  .  c o  m
     * Adjusts the Calendar to several days before or after the current date.
     *
     * @param calendar        the Calendar object to be adjusted.
     * @param differenceInDay the difference in days. It accepts both position and negative number.
     * @return the calendar after the adjustment. It should always be the same instance as the calendar parameter.
     */
    public static Calendar adjustDate(Calendar calendar, int differenceInDay) {
        calendar.setTimeInMillis(calendar.getTimeInMillis() + DAY_IN_MS * differenceInDay);
        return calendar;
    }
}

Related

  1. adjustCalendar(Calendar calendar)
  2. adjustCalendarToLocalTime(Calendar cal)
  3. adjustedMillis(Calendar cal)
  4. adjustToDayEnd(Calendar cal)
  5. adjustToFieldStart(Calendar cal, int[] fields)
  6. adjustToMonthStart(Calendar cal)