Java TimeUnit Calculate between(final Calendar calendar1, final Calendar calendar2, final TimeUnit scale)

Here you can find the source of between(final Calendar calendar1, final Calendar calendar2, final TimeUnit scale)

Description

Compare two dates and returns the duration between them following the time scale.

License

Apache License

Parameter

Parameter Description
calendar1 first date
calendar2 second date
scale the time scale

Return

the duration (can be negative)

Declaration

public static long between(final Calendar calendar1, final Calendar calendar2, final TimeUnit scale) 

Method Source Code

//package com.java2s;
/*// www .ja va  2 s  .com
 * #%L
 * utils-commons
 * %%
 * Copyright (C) 2016 - 2018 Gilles Landel
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.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.
 * #L%
 */

import java.util.Calendar;
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

public class Main {
    /**
     * Compare two dates and returns the duration between them following the
     * time scale.
     * 
     * <p>
     * precondition: {@code date1} cannot be {@code null}, {@code date2} cannot
     * be {@code null} and {@code scale} cannot be {@code null}
     * </p>
     * 
     * @param date1
     *            first date
     * @param date2
     *            second date
     * @param scale
     *            the time scale
     * @return the duration (can be negative)
     */
    public static long between(final Date date1, final Date date2, final TimeUnit scale) {
        Objects.requireNonNull(date1, "The parameter date1 cannot be null");
        Objects.requireNonNull(date2, "The parameter date2 cannot be null");
        Objects.requireNonNull(scale, "The parameter scale cannot be null");

        return scale.convert(date2.getTime() - date1.getTime(), TimeUnit.MILLISECONDS);
    }

    /**
     * Compare two dates and returns the duration between them following the
     * time scale.
     * 
     * <p>
     * precondition: {@code calendar1} cannot be {@code null}, {@code calendar2}
     * cannot be {@code null} and {@code scale} cannot be {@code null}
     * </p>
     * 
     * @param calendar1
     *            first date
     * @param calendar2
     *            second date
     * @param scale
     *            the time scale
     * @return the duration (can be negative)
     */
    public static long between(final Calendar calendar1, final Calendar calendar2, final TimeUnit scale) {
        Objects.requireNonNull(calendar1, "The parameter calendar1 cannot be null");
        Objects.requireNonNull(calendar2, "The parameter calendar2 cannot be null");

        return between(calendar1.getTime(), calendar2.getTime(), scale);
    }
}

Related

  1. abbreviate(final TimeUnit unit)
  2. addTimeUnit(Calendar cal, int unit, int val)
  3. appendTimeout(StringBuilder b, long timeout, TimeUnit unit)
  4. calculateDateDiff(Date date1, Date date2, TimeUnit timeUnit)
  5. calculateDelatBasedOnRate(int rateLimit, TimeUnit periodUnit, int period, TimeUnit resultingTimeUnit)
  6. calculateTimeout(long timeout, TimeUnit timeUnit)
  7. checkTime(long time, TimeUnit timeunit)