Java Calendar Different diffDay(Calendar calendar, int day)

Here you can find the source of diffDay(Calendar calendar, int day)

Description

diff Day

License

MIT License

Declaration

public static Calendar diffDay(Calendar calendar, int day) 

Method Source Code

//package com.java2s;
/*!//from ww  w. j  a va2  s  .c o  m
 * mifmi-commons4j
 * https://github.com/mifmi/mifmi-commons4j
 *
 * Copyright (c) 2015 mifmi.org and other contributors
 * Released under the MIT license
 * https://opensource.org/licenses/MIT
 */

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

import java.util.Locale;

import java.util.TimeZone;

public class Main {
    public static long diffDay(long date, int day, TimeZone timeZone, Locale locale) {
        return diff(date, 0, 0, day, 0, 0, 0, 0, timeZone, locale);
    }

    public static Date diffDay(Date date, int day, TimeZone timeZone, Locale locale) {
        return diff(date, 0, 0, day, 0, 0, 0, 0, timeZone, locale);
    }

    public static Calendar diffDay(Calendar calendar, int day) {
        return diff(calendar, 0, 0, day, 0, 0, 0, 0);
    }

    public static long diff(long date, int year, int month, int day, int hour, int minute, int second,
            int milliSecond, TimeZone timeZone, Locale locale) {
        Calendar calendar = getCalendar(timeZone, locale);
        calendar.setTimeInMillis(date);
        calendar = diff(calendar, year, month, day, hour, minute, second, milliSecond);
        return calendar.getTimeInMillis();
    }

    public static Date diff(Date date, int year, int month, int day, int hour, int minute, int second,
            int milliSecond, TimeZone timeZone, Locale locale) {
        Calendar calendar = getCalendar(timeZone, locale);
        calendar.setTime(date);
        calendar = diff(calendar, year, month, day, hour, minute, second, milliSecond);
        return calendar.getTime();
    }

    public static Calendar diff(Calendar calendar, int year, int month, int day, int hour, int minute, int second,
            int milliSecond) {
        calendar.add(Calendar.MILLISECOND, milliSecond);
        calendar.add(Calendar.SECOND, second);
        calendar.add(Calendar.MINUTE, minute);
        calendar.add(Calendar.HOUR, hour);
        calendar.add(Calendar.DAY_OF_MONTH, day);
        calendar.add(Calendar.MONTH, month);
        calendar.add(Calendar.YEAR, year);
        return calendar;
    }

    public static Calendar getCalendar(TimeZone timeZone, Locale locale) {
        Calendar cal;
        if (timeZone == null) {
            if (locale == null) {
                cal = Calendar.getInstance();
            } else {
                cal = Calendar.getInstance(locale);
            }
        } else {
            if (locale == null) {
                cal = Calendar.getInstance(timeZone);
            } else {
                cal = Calendar.getInstance(timeZone, locale);
            }
        }
        return cal;
    }
}

Related

  1. dateDiff(int type, Calendar fromDate, Calendar toDate, boolean future)
  2. dateDiff(int type, Calendar fromDate, Calendar toDate, boolean future)
  3. dateDiff(long dateUtilitiesUnitField, Calendar firstDate, Calendar secondDate)
  4. dateDifferenceInMin(Calendar startDate, Calendar stopDate)
  5. diff(Calendar value1, Calendar value2, int millisInCondition)
  6. diffDays(Calendar startDate, Calendar endDate)
  7. differenceInDays(Calendar date1, Calendar date2)
  8. formatTimeDiff(Calendar ref, Calendar now, boolean ignoreMS)
  9. getDateDiff(Calendar c1, Calendar c2)