Here you can find the source of elapsedMillis(Calendar before, Calendar after)
Parameter | Description |
---|---|
before | The first calendar with expected date before the second calendar. |
after | The second calendar with expected date after the first calendar. |
Parameter | Description |
---|---|
IllegalArgumentException | If the first calendar is dated after the second calendar. |
public static long elapsedMillis(Calendar before, Calendar after)
//package com.java2s; //License from project: Apache License import java.util.Calendar; public class Main { /**/*from w w w . j a va 2 s .c o m*/ * 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. * @throws IllegalArgumentException If the first calendar is dated after the second calendar. */ 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. * @throws IllegalArgumentException If the first calendar is dated after the second calendar or * if the division factor is less than 1. */ 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. * @throws IllegalArgumentException If the first calendar is dated after the second 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."); } } }