Here you can find the source of isDateInInterval(Calendar date, int interval, TimeUnit type)
Parameter | Description |
---|---|
date | The input date |
interval | The interval |
type | The type (ex. TimeUnit.DAYS, TimeUnit.MONTHS |
public static boolean isDateInInterval(Calendar date, int interval, TimeUnit type)
//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()); } }