How to check if date is between interval in java?

B. Hurray :

I have six int variables: currentMonth, currentDay, monthFrom, dayFrom, monthUntil and dayUntil. I need to check if todays month and day falls within a range of from and until variables.

For example if currentMonth = 1, currentDay = 2, monthFrom = 11, dayFrom = 24, monthUntil = 3 and dayUntil = 3 the date is in the interval and the method should return true.

I'm not sure how to do it though. Is there any other option than to check every possible outcome using ifs?

user5549921 :

Just do a quick range check with the calendar:

Note: Make sure to import java.util.GregorianCalendar;

public static boolean isDateInRange(int month, int day,
                                    int monthFrom, int dayFrom,
                                    int monthUntil, int dayUntil) {
    int yearRoll = 0;
    int currentRoll = 0;
    if (monthUntil < monthFrom) yearRoll = -1; // Ensures date is calculated correctly.
    if (month >= monthFrom && yearRoll < 0) currentRoll = -1;

    GregorianCalendar testDate = new GregorianCalendar(currentRoll, month, day);
    GregorianCalendar startDate = new GregorianCalendar(yearRoll, monthFrom, dayFrom);
    GregorianCalendar endDate = new GregorianCalendar(0, monthUntil, dayUntil);

    // This makes it pass if its between OR EQUAL to the interval.
    // Remove if you only want to pass dates explicitly BETWEEN intervals.
    if (testDate.compareTo(startDate) == 0 || testDate.compareTo(endDate) == 0) {
        return true;
    }

    return !(testDate.before(startDate) || testDate.after(endDate));
}

This will also take into account the fact that say February is between November and March. Since November is a part of the previous year, it will move the from date back a year to ensure passing.

What it doesn't take into account however, is the fact that February has an extra day on leap-years. To add extra-precision, you need integers for the years. You can do the following:

public static boolean isDateInRange(int year, int month, int day,
                                    int yearFrom, int monthFrom, int dayFrom,
                                    int yearUntil, int monthUntil, int dayUntil) {

    GregorianCalendar testDate = new GregorianCalendar(year, month, day);
    GregorianCalendar startDate = new GregorianCalendar(yearFrom, monthFrom, dayFrom);
    GregorianCalendar endDate = new GregorianCalendar(yearUntil, monthUntil, dayUntil);

    return !(testDate.before(startDate) || testDate.after(endDate));
}

And here is an implementation with the date values you gave plus a few more:

public static void main(String[] args) {
    System.out.println(isDateInRange(1, 2,
                                     11, 24,
                                     3, 3));
    System.out.println(isDateInRange(11, 25,
                                     11, 24,
                                     3, 3));
    System.out.println(isDateInRange(1, 2,
                                     1, 1,
                                     3, 3));
    System.out.println(isDateInRange(1, 22,
                                     1, 21,
                                     1, 25));
}

And the results are:

true
true
true
true

Will also work with @Marvin's tests.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=476927&siteId=1