How to check if interval between intervals exists in java?

I have six variables int: currentMonth, currentDay, monthFrom, dayFrom, monthUntiland dayUntil. I need to check if the current months and days are in the range from and to variables.

For example, if currentMonth = 1, currentDay = 2, monthFrom = 11, dayFrom = 24, monthUntil = 3and dayUntil = 3date ranges, and the method should return a value of true.

I am not sure how to do this. Is there any other option than checking all possible results with ifs?

+6
source share
4 answers

Just take a quick look at the calendar:

Note. Make sure thatimport 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));
}

, - . , from , .

, . , . :

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));
}

, , :

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));
}

:

true
true
true
true

@Marvin.

+3

java.time.MonthDay . :

  • from until, from <= current && current <= until.
  • from until, current <= until || current >= from

:

public static boolean isBetween(
    int currentMonth, int currentDay,
    int fromMonth, int fromDay,
    int untilMonth, int untilDay)
{
    MonthDay current = MonthDay.of(currentMonth, currentDay);
    MonthDay from = MonthDay.of(fromMonth, fromDay);
    MonthDay until = MonthDay.of(untilMonth, untilDay);

    if (from.compareTo(until) <= 0)
    {
        return from.compareTo(current) <= 0 &&
            current.compareTo(until) <= 0;
    }
    else
    {
        return current.compareTo(until) <= 0 ||
            current.compareTo(from) >= 0;
    }
}

return , , , .

( , .)

+5

LocalDate java . , , .

import java.time.LocalDate;

public class DateBetween {

    public static void main(String[] args) {
        System.out.println(isBetween(1, 2, 11, 24, 3, 3)); // true
        System.out.println(isBetween(4, 4, 1, 20, 6, 3)); // true
        System.out.println(isBetween(1, 1, 2, 3, 7, 8)); // false
        System.out.println(isBetween(11, 4, 2, 3, 7, 8)); // false
        System.out.println(isBetween(2, 29, 2, 3, 7, 8)); // true
        System.out.println(isBetween(2, 29, 11, 24, 3, 3)); // true
        System.out.println(isBetween(1, 1, 1, 1, 1, 1)); // true
    }

    private static boolean isBetween(int currentMonth, int currentDay, int monthFrom, int dayFrom, int monthUntil,
            int dayUntil) {
        // Default to 2000 so that Feb 29st will be valid.
        int currentYear = 2000;
        LocalDate dateFrom = LocalDate.of(currentYear, monthFrom, dayFrom);
        LocalDate dateUntil = LocalDate.of(currentYear, monthUntil, dayUntil);
        if (dateFrom.isAfter(dateUntil)) {
            // Assume dateUntil is in the next year (e.g. for 11/24/2000 -
            // 6/3/2001)
            dateUntil = dateUntil.plusYears(1);
        }
        // Day to check in current year (for non-overlapping ranges)
        LocalDate currentDateThisYear = LocalDate.of(currentYear, currentMonth, currentDay);
        // Day to check in next year (for overlapping ranges)
        LocalDate currentDateNextYear = currentDateThisYear.plusYears(1);
        if (!(currentDateThisYear.isBefore(dateFrom) || currentDateThisYear.isAfter(dateUntil))) {
            return true;
        } else if (!(currentDateNextYear.isBefore(dateFrom) || currentDateNextYear.isAfter(dateUntil))) {
            return true;
        }
        // Neither of the days to check are in the range
        return false;
    }
}

, dateUntil , dateFrom. "" , , . "1 " (2017) "1 " (2018), "24 " "1 ".

, , currentYear, yearFrom yearUntil.

+2

.

GregorianCalendar, LocalDate, , . , 29 , .

, , , , , int, ? , , MonthDay, int s. , Android ThreeTenABP, , : , Java 8 Android java.time .

, MonthDay, - . , 32 . , , : , .

public static boolean isBetween(int currentMonth, int currentDay, 
        int monthFrom, int dayFrom, 
        int monthUntil, int dayUntil) {
    final int daysInMonth = 32; // just imagine; pick any number >= 31
    int current = currentMonth * daysInMonth + currentDay;
    int from = monthFrom * daysInMonth + dayFrom;
    int until = monthUntil * daysInMonth + dayUntil;

    if (until < from) { // until is in the following year
        return current >= from || current <= until;
    } else {
        return current >= from && current <= until;
    }
}

, < <= .. .

( ) - . isBetween(55, -45, 83, 98, -29, 84), false, true , - .

: java.time Android ThreeTenABP.

0

Source: https://habr.com/ru/post/1016542/


All Articles