Android Calendar Compare isDateInInterval(Calendar date, int interval, TimeUnit type)

Here you can find the source of isDateInInterval(Calendar date, int interval, TimeUnit type)

Description

Check if a given date lies in a given interval from today's date.

License

MIT License

Parameter

Parameter Description
date The input date
interval The interval
type The type (ex. TimeUnit.DAYS, TimeUnit.MONTHS

Return

True if date lies within the interval, otherwise false

Declaration

public static boolean isDateInInterval(Calendar date, int interval,
        TimeUnit type) 

Method Source Code

//package com.java2s;
/**//from   w  ww . ja v a  2s.  com
 *   DateTimeUtils.java
 *
 *  A utility class for converting dates to common formats
 *
 *   @author Robin Andersson, Johan Brook
 *   @copyright (c) 2012 Robin Andersson, Johan Brook, Mattias Henriksson, Lisa Stenberg
 *   @license MIT
 */

import java.util.Calendar;

import java.util.concurrent.TimeUnit;

public class Main {
    /**
     * Check if a given date lies in a given interval from today's date.
     * 
     * <p>Ex. Check if a date is 5 days from now:</p>
     * 
     * <pre>DateTimeUtils.isDateInInterval(aDate, 5, TimeUnit.DAYS)
     * </pre>
     * 
     * @param date The input date
     * @param interval The interval
     * @param type The type (ex. TimeUnit.DAYS, TimeUnit.MONTHS
     * @return True if date lies within the interval, otherwise false
     */
    public static boolean isDateInInterval(Calendar date, int interval,
            TimeUnit type) {
        Calendar diff = Calendar.getInstance();
        long today = Calendar.getInstance().getTimeInMillis();

        diff.setTimeInMillis(TimeUnit.MILLISECONDS.convert(interval, type));

        return (date.getTimeInMillis() - today < diff.getTimeInMillis());
    }
}

Related

  1. isSameDay(Calendar calSource, Calendar calDesti)
  2. isSameInstant(Calendar cal1, Calendar cal2)
  3. isSameLocalTime(Calendar calSource, Calendar calDesti)
  4. compare(Calendar d1, Calendar d2)
  5. getDateDelta(int delta, String dateFormat)
  6. isCurrentTimeInRange(Calendar now, Calendar start, Calendar end)
  7. isFiveMinutesOlder(Calendar updatedAt)
  8. isPast(String end)
  9. isOverCurrentDate(int day, int month, int year, int maxDay, int maxMonth, int maxYear)