Java Calendar Calculate postpone(final Calendar calendar, final int measureUnit, final int amount)

Here you can find the source of postpone(final Calendar calendar, final int measureUnit, final int amount)

Description

postpone

License

LGPL

Declaration

public static Date postpone(final Calendar calendar, final int measureUnit, final int amount) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.util.*;

public class Main {
    /**/*from  w  w  w  . j a  v a2s. c om*/
     * Milliseconds=14 *
     */
    public static final int MILLISECOND = Calendar.MILLISECOND;
    /**
     * Seconds=13 *
     */
    public static final int SECOND = Calendar.SECOND;
    /**
     * Minute=12 *
     */
    public static final int MINUTE = Calendar.MINUTE;
    /**
     * Hour=11 *
     */
    public static final int HOUR = Calendar.HOUR_OF_DAY;
    /**
     * Day=5 *
     */
    public static final int DAY = Calendar.DAY_OF_MONTH;
    /**
     * MONTH=2 *
     */
    public static final int MONTH = Calendar.MONTH;
    /**
     * Year=1 *
     */
    public static final int YEAR = Calendar.YEAR;

    public static Date postpone(final Date date, final int measureUnit, final int amount) {
        final Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        return postpone(calendar, measureUnit, amount);
    }

    public static Date postpone(final Calendar calendar, final int measureUnit, final int amount) {

        switch (measureUnit) {
        case MILLISECOND:
            calendar.add(MILLISECOND, amount);
            break;
        case SECOND:
            calendar.add(SECOND, amount);
            break;
        case MINUTE:
            calendar.add(MINUTE, amount);
            break;
        case HOUR:
            calendar.add(HOUR, amount);
            break;
        case DAY:
            calendar.add(DAY, amount);
            break;
        case MONTH:
            calendar.add(MONTH, amount);
            break;
        case YEAR:
            calendar.add(YEAR, amount);
            break;
        }

        return calendar.getTime();
    }
}

Related

  1. isSameLocalTime(Calendar cal1, Calendar cal2)
  2. maximizeTime(Calendar cal)
  3. millisToCalendar(long millis)
  4. normalize(final Calendar cal)
  5. normalizedClone(final Calendar cal)
  6. resetCalendarTime(Calendar calendar)
  7. roundBack(Calendar start)
  8. roundCalToNextQuarterHour(GregorianCalendar cal)
  9. transform(Calendar cal)