Java TimeUnit Calculate addTimeUnit(Calendar cal, int unit, int val)

Here you can find the source of addTimeUnit(Calendar cal, int unit, int val)

Description

Add a quantity of time units to a Calendar

License

Open Source License

Parameter

Parameter Description
cal Calendar to add to
unit Calendar unit to add
val Quantity of unit to add

Declaration

public static void addTimeUnit(Calendar cal, int unit, int val) 

Method Source Code


//package com.java2s;
/*//from w  w w  . j a  v  a 2 s  .  co m
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 *
 *    (C) 2002-2014, Open Source Geospatial Foundation (OSGeo)
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */

import java.util.Calendar;

import java.util.concurrent.TimeUnit;

public class Main {
    /**
     * Add a quantity of time units to a Calendar
     * 
     * @param cal Calendar to add to
     * @param unit Calendar unit to add
     * @param val Quantity of unit to add
     */
    public static void addTimeUnit(Calendar cal, int unit, int val) {
        addTimeUnit(cal, unit, (long) val);
    }

    /**
     * Add a quantity of time units to a Calendar
     * 
     * @param cal Calendar to add to
     * @param unit Calendar unit to add
     * @param val Quantity of TimeUnit to add
     */
    public static void addTimeUnit(Calendar cal, int unit, long val) {
        if (unit == Calendar.DATE) {
            cal.setTimeInMillis(cal.getTimeInMillis() + TimeUnit.DAYS.toMillis(val));
        } else if (unit == Calendar.HOUR) {
            cal.setTimeInMillis(cal.getTimeInMillis() + TimeUnit.HOURS.toMillis(val));
        } else if (unit == Calendar.MINUTE) {
            cal.setTimeInMillis(cal.getTimeInMillis() + TimeUnit.MINUTES.toMillis(val));
        } else if (unit == Calendar.SECOND) {
            cal.setTimeInMillis(cal.getTimeInMillis() + TimeUnit.SECONDS.toMillis(val));
        } else if (unit == Calendar.MILLISECOND) {
            cal.setTimeInMillis(cal.getTimeInMillis() + val);
        } else {
            int intVal = (int) val;
            if (intVal != val) {
                throw new IllegalArgumentException("Can't convert " + val + " to an int without losing data");
            }
            cal.add(unit, intVal);
        }
    }
}

Related

  1. abbreviate(final TimeUnit unit)
  2. appendTimeout(StringBuilder b, long timeout, TimeUnit unit)
  3. between(final Calendar calendar1, final Calendar calendar2, final TimeUnit scale)
  4. calculateDateDiff(Date date1, Date date2, TimeUnit timeUnit)
  5. calculateDelatBasedOnRate(int rateLimit, TimeUnit periodUnit, int period, TimeUnit resultingTimeUnit)