Android To check within a period


Example

This example will help to verify the given time is within a period or not.

To check the time is today, We can use DateUtils class

boolean isToday = DateUtils.isToday(timeInMillis);

To check the time is within a week,

private static boolean isWithinWeek(final long millis) {
    return System.currentTimeMillis() - millis <= (DateUtils.WEEK_IN_MILLIS - DateUtils.DAY_IN_MILLIS);
}

To check the time is within a year,

private static boolean isWithinYear(final long millis) {
    return System.currentTimeMillis() - millis <= DateUtils.YEAR_IN_MILLIS;
}

To check the time is within a number day of day including today,

public static boolean isWithinDay(long timeInMillis, int day) {
    long diff = System.currentTimeMillis() - timeInMillis;

    float dayCount = (float) (diff / DateUtils.DAY_IN_MILLIS);

    return dayCount < day;
}

Note : DateUtils is android.text.format.DateUtils