Java Calendar Different getDifference(int constant, Calendar first, Calendar second)

Here you can find the source of getDifference(int constant, Calendar first, Calendar second)

Description

Get the difference in time between two calendars for an individual time unit.

License

CDDL license

Parameter

Parameter Description
constant the constant representing the time unit to compare
first the first Calendar object
second the second Calendar object

Return

the number of the specified time units between the first and second Calendar objects

Declaration

private static int getDifference(int constant, Calendar first, Calendar second) 

Method Source Code


//package com.java2s;
/*//  w  w w  .  java  2s  .  c  o m
 * This document is a part of the source code and related artifacts for StilesLib, an open source library that
 * provides a set of commonly-used functions for Bukkit plugins.
 *
 * http://github.com/mstiles92/StilesLib
 *
 * Copyright (c) 2014 Matthew Stiles (mstiles92)
 *
 * Licensed under the Common Development and Distribution License Version 1.0
 * You may not use this file except in compliance with this License.
 *
 * You may obtain a copy of the CDDL-1.0 License at http://opensource.org/licenses/CDDL-1.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the license.
 */

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

public class Main {
    /**
     * Get the difference in time between two calendars for an individual time unit.
     *
     * @param constant the constant representing the time unit to compare
     * @param first the first Calendar object
     * @param second the second Calendar object
     * @return the number of the specified time units between the first and second Calendar objects
     */
    private static int getDifference(int constant, Calendar first, Calendar second) {
        int difference = 0;
        Calendar temp = new GregorianCalendar();
        temp.setTimeInMillis(first.getTimeInMillis());

        while (!temp.after(second)) {
            temp.add(constant, 1);
            difference += 1;
        }

        return difference - 1;
    }
}

Related

  1. formatTimeDiff(Calendar ref, Calendar now, boolean ignoreMS)
  2. getDateDiff(Calendar c1, Calendar c2)
  3. getDateDiff(Calendar d1, Calendar d2)
  4. getDateDiff(Date d1, Date d2, int gregorianCalendarUnits)
  5. getDaysDifference(Calendar refDate, Date date)
  6. getDifference(int field, Calendar aCalX, Calendar aCalY)
  7. getDifferenceInDays(Calendar fromDate, Calendar toDate)
  8. getDifferenceInSeconds(Calendar calendarStart, Calendar calendarEnd)
  9. getDiffPartialYears(Date date, GregorianCalendar b)