Java Calendar Add addDayOffset(final Calendar date, long offset)

Here you can find the source of addDayOffset(final Calendar date, long offset)

Description

add Day Offset

License

Open Source License

Declaration

private static Calendar addDayOffset(final Calendar date, long offset) 

Method Source Code


//package com.java2s;
/*// ww w .j a v a  2s .com
 * 
 * Copyright (c) 2011 by Jgility Development Group
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Karsten Schulz
 *
 */

import java.util.Calendar;
import java.util.GregorianCalendar;

public class Main {
    private static Calendar addDayOffset(final Calendar date, long offset) {
        Calendar newDate = new GregorianCalendar();
        newDate.setTimeInMillis(date.getTimeInMillis());
        newDate.add(Calendar.DAY_OF_MONTH, safeLongToInt(offset));
        return newDate;
    }

    private static int safeLongToInt(long l) {
        if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
            throw new IllegalArgumentException(l + " cannot be cast to int without changing its value.");
        }
        return (int) l;
    }
}

Related

  1. addCalendarQuarterOfYear(Calendar cal, int quartersOfYear)
  2. addDate(Calendar baseDate, int addDate)
  3. addDate(Calendar baseDate, int diffDate, TimeZone timezone)
  4. addDate(Date date, int calendarField, int amount)
  5. addDate(Date date, int calendarField, int numberToAdd)
  6. addDays(Calendar aTarget, int aAddDays)
  7. addDays(Calendar calendar, int days)
  8. addDays(Calendar src, int days)
  9. addDays(final int days, final Calendar from)