Calculate the total of elapsed time from years up to seconds between the two given calendars. - Java java.util

Java examples for java.util:Year

Description

Calculate the total of elapsed time from years up to seconds between the two given calendars.

Demo Code


//package com.java2s;
import java.util.Calendar;

public class Main {
    public static void main(String[] argv) throws Exception {
        Calendar before = Calendar.getInstance();
        Calendar after = Calendar.getInstance();
        System.out.println(java.util.Arrays.toString(elapsedTime(before,
                after)));/*from ww w.  ja  v a 2 s . co m*/
    }

    /**
     * Calculate the total of elapsed time from years up to seconds between the
     * two given calendars. It returns an int array with the elapsed years,
     * months, days, hours, minutes and seconds respectively.
     *
     * @param before The first calendar with expected date before the second
     * calendar.
     * @param after The second calendar with expected date after the first
     * calendar.
     * @return The elapsed time between the two given calendars in years,
     * months, days, hours, minutes and seconds.
     */
    public static int[] elapsedTime(Calendar before, Calendar after) {
        int[] elapsedTime = new int[6];
        Calendar clone = (Calendar) before.clone(); // Otherwise changes are
        // been reflected.

        elapsedTime[0] = elapsedYears(clone, after);
        addYears(clone, elapsedTime[0]);

        elapsedTime[1] = elapsedMonths(clone, after);
        addMonths(clone, elapsedTime[1]);

        elapsedTime[2] = elapsedDays(clone, after);
        addDays(clone, elapsedTime[2]);

        elapsedTime[3] = elapsedHours(clone, after);
        addHours(clone, elapsedTime[3]);

        elapsedTime[4] = elapsedMinutes(clone, after);
        addMinutes(clone, elapsedTime[4]);

        elapsedTime[5] = elapsedSeconds(clone, after);

        return elapsedTime;
    }

    /**
     * Retrieve the amount of elapsed years between the two given calendars.
     *
     * @param before The first calendar with expected date before the second
     * calendar.
     * @param after The second calendar with expected date after the first
     * calendar.
     * @return The amount of elapsed years between the two given calendars.
     */
    public static int elapsedYears(Calendar before, Calendar after) {
        return elapsed(before, after, Calendar.YEAR);
    }

    /**
     * Add the given amount of years to the given calendar. The changes are
     * reflected in the given calendar.
     * 
     * @param calendar
     *            The calendar to add the given amount of years to.
     * @param years
     *            The amount of years to be added to the given calendar.
     *            Negative values are also allowed, it will just go back in
     *            time.
     */
    public static void addYears(Calendar calendar, int years) {
        calendar.add(Calendar.YEAR, years);
    }

    /**
     * Retrieve the amount of elapsed months between the two given calendars.
     *
     * @param before The first calendar with expected date before the second
     * calendar.
     * @param after The second calendar with expected date after the first
     * calendar.
     * @return The amount of elapsed months between the two given calendars.
     */
    public static int elapsedMonths(Calendar before, Calendar after) {
        return elapsed(before, after, Calendar.MONTH);
    }

    /**
     * Add the given amount of months to the given calendar. The changes are
     * reflected in the given calendar.
     * 
     * @param calendar
     *            The calendar to add the given amount of months to.
     * @param months
     *            The amount of months to be added to the given calendar.
     *            Negative values are also allowed, it will just go back in
     *            time.
     */
    public static void addMonths(Calendar calendar, int months) {
        calendar.add(Calendar.MONTH, months);
    }

    /**
     * Retrieve the amount of elapsed days between the two given calendars.
     *
     * @param before The first calendar with expected date before the second
     * calendar.
     * @param after The second calendar with expected date after the first
     * calendar.
     * @return The amount of elapsed days between the two given calendars.
     */
    public static int elapsedDays(Calendar before, Calendar after) {
        return elapsed(before, after, Calendar.DATE);
    }

    /**
     * Add the given amount of days to the given calendar. The changes are
     * reflected in the given calendar.
     * 
     * @param calendar
     *            The calendar to add the given amount of days to.
     * @param days
     *            The amount of days to be added to the given calendar. Negative
     *            values are also allowed, it will just go back in time.
     */
    public static void addDays(Calendar calendar, int days) {
        calendar.add(Calendar.DATE, days);
    }

    /**
     * Retrieve the amount of elapsed hours between the two given calendars.
     *
     * @param before The first calendar with expected date before the second
     * calendar.
     * @param after The second calendar with expected date after the first
     * calendar.
     * @return The amount of elapsed hours between the two given calendars.
     */
    public static int elapsedHours(Calendar before, Calendar after) {
        return (int) elapsedMillis(before, after, 3600000); // 1h = 60m = 3600s
        // = 3600000ms
    }

    /**
     * Add the given amount of hours to the given calendar. The changes are
     * reflected in the given calendar.
     * 
     * @param calendar
     *            The calendar to add the given amount of hours to.
     * @param hours
     *            The amount of hours to be added to the given calendar.
     *            Negative values are also allowed, it will just go back in
     *            time.
     */
    public static void addHours(Calendar calendar, int hours) {
        calendar.add(Calendar.HOUR, hours);
    }

    /**
     * Retrieve the amount of elapsed minutes between the two given calendars.
     *
     * @param before The first calendar with expected date before the second
     * calendar.
     * @param after The second calendar with expected date after the first
     * calendar.
     * @return The amount of elapsed minutes between the two given calendars.
     */
    public static int elapsedMinutes(Calendar before, Calendar after) {
        return (int) elapsedMillis(before, after, 60000); // 1m = 60s = 60000ms
    }

    /**
     * Add the given amount of minutes to the given calendar. The changes are
     * reflected in the given calendar.
     * 
     * @param calendar
     *            The calendar to add the given amount of minutes to.
     * @param minutes
     *            The amount of minutes to be added to the given calendar.
     *            Negative values are also allowed, it will just go back in
     *            time.
     */
    public static void addMinutes(Calendar calendar, int minutes) {
        calendar.add(Calendar.MINUTE, minutes);
    }

    /**
     * Retrieve the amount of elapsed seconds between the two given calendars.
     *
     * @param before The first calendar with expected date before the second
     * calendar.
     * @param after The second calendar with expected date after the first
     * calendar.
     * @return The amount of elapsed seconds between the two given calendars.
     */
    public static int elapsedSeconds(Calendar before, Calendar after) {
        return (int) elapsedMillis(before, after, 1000); // 1sec = 1000ms.
    }

    /**
     * Retrieve the amount of elapsed time between the two given calendars based
     * on the given calendar field as defined in the Calendar constants, e.g.
     * <tt>Calendar.MONTH</tt>.
     *
     * @param before The first calendar with expected date before the second
     * calendar.
     * @param after The second calendar with expected date after the first
     * calendar.
     * @param field The calendar field as defined in the Calendar constants.
     * @return The amount of elapsed time between the two given calendars based
     * on the given calendar field.
     */
    private static int elapsed(Calendar before, Calendar after, int field) {
        checkBeforeAfter(before, after);
        Calendar clone = (Calendar) before.clone(); // Otherwise changes are
        // been reflected.
        int elapsed = -1;
        while (!clone.after(after)) {
            clone.add(field, 1);
            elapsed++;
        }
        return elapsed;
    }

    /**
     * Retrieve the amount of elapsed milliseconds between the two given
     * calendars.
     *
     * @param before The first calendar with expected date before the second
     * calendar.
     * @param after The second calendar with expected date after the first
     * calendar.
     * @return The amount of elapsed milliseconds between the two given
     * calendars.
     */
    public static long elapsedMillis(Calendar before, Calendar after) {
        return elapsedMillis(before, after, 1); // 1ms is apparently 1ms.
    }

    /**
     * Retrieve the amount of elapsed milliseconds between the two given
     * calendars and directly divide the outcome by the given factor. E.g.: if
     * the division factor is 1, then you will get the elapsed milliseconds
     * unchanged; if the division factor is 1000, then the elapsed milliseconds
     * will be divided by 1000, resulting in the amount of elapsed seconds.
     *
     * @param before The first calendar with expected date before the second
     * calendar.
     * @param after The second calendar with expected date after the first
     * calendar.
     * @param factor The division factor which to divide the milliseconds with,
     * expected to be at least 1.
     * @return The amount of elapsed milliseconds between the two given
     * calendars, divided by the given factor.
     */
    private static long elapsedMillis(Calendar before, Calendar after,
            int factor) {
        checkBeforeAfter(before, after);
        if (factor < 1) {
            throw new IllegalArgumentException("Division factor '" + factor
                    + "' should not be less than 1.");
        }
        return (after.getTimeInMillis() - before.getTimeInMillis())
                / factor;
    }

    /**
     * Check if the first calendar is actually dated before the second calendar.
     *
     * @param before The first calendar with expected date before the second
     * calendar.
     * @param after The second calendar with expected date after the first
     * calendar.
     */
    private static void checkBeforeAfter(Calendar before, Calendar after) {
        if (before.after(after)) {
            throw new IllegalArgumentException(
                    "The first calendar should be dated before the second calendar.");
        }
    }
}

Related Tutorials