How to check if current date exists between two duplicate dates in java?

I am trying to create an application and am stuck in calculating if today is the school year. The user enters two dates, without any year, which are repeated annually . These are the start and end dates of the school year.

I want to check if the current date is between the two, even if it overlaps two years. Therefore, if the school begins in November and ends in June, if today is January 22, it should return to the truth. But if its July, it should return false.

I found this question: Php - work out what school year it is , but it works in the academic years, which have no holiday.

By the way, I have time in joda if that helps.

Thanks in advance.

-2
source share
3 answers

Well, I thought a little and found some solutions, but it seemed the simplest.

private boolean isInSchoolYear(DateTime now, DateTime schoolYearStart, DateTime schoolYearEnd){
    int thisYear = now.getYear();
    schoolYearStart = schoolYearStart.withYear(thisYear);
    schoolYearEnd = schoolYearEnd.withYear(thisYear);

    if(schoolYearStart.isBefore(schoolYearEnd)){
        return new Interval(schoolYearStart, schoolYearEnd).contains(now);
    }
    else{
        return !(new Interval(schoolYearEnd, schoolYearStart).contains(now));
    }
}

Thus, it works as if the school year is superimposed on 2 years or 1.

0
source
//parse both enddate/startdate to same year

if(enddate>startdate)
enddate+=1 year;

if(today>startdate and today<enddate)
return true;
0
source

.

, , . .

, :

, (, ) , ( "-" ), (), () . , .

Joda

, Joda-Time 2.7. :

  • , - , . , Joda-Time.
  • . .

, Joda-Time MonthDay, .

MonthDay mdStart = new MonthDay ( DateTimeConstants.NOVEMBER, 1 );
MonthDay mdStop = new MonthDay ( DateTimeConstants.JUNE, 1 );

, , . , " ". , 3-4 .

DateTime target = DateTime.now ( DateTimeZone.forID ( "Africa/Casablanca" ) ); // Using current date-time, but could be any date-time.

toDateTime MonthDay ( ) DateTime, DateTime. Joda-Time , , .

:

  • MonthDay - , DateTime. ( )
  • MonthDay - , DateTime. ( )

-, .

// Determine a span of time, moving the Start to previous year if need be.
DateTime earlierStart = mdStart.toDateTime ( target ); // Overlay the data fields of the MonthDay on top of the data fields of a DateTime to create a new DateTime object.
DateTime earlierStop = mdStop.toDateTime ( target );
if ( earlierStart.isAfter ( earlierStop ) ) {  // If the start would land after the stop, then adjust the start to *previous* year.
    earlierStart = earlierStart.minusYears ( 1 );
}

Joda-Time Interval .

Interval earlierInterval = new Interval ( earlierStart, earlierStop );

// Determine a span of time, moving the Stop to next year if need be.
DateTime laterStart = mdStart.toDateTime ( target ); // Overlay the data fields of the MonthDay on top of the data fields of a DateTime to create a new DateTime object.
DateTime laterStop = mdStop.toDateTime ( target );
if ( laterStart.isAfter ( laterStop ) ) {  // If the start would land after the stop, then adjust the start to *next* year.
    laterStop = laterStop.plusYears ( 1 );
}
Interval laterInterval = new Interval ( laterStart, laterStop );

, DateTime Interval. , Joda-Time Half-Open , , . , 1 . , 1 , , 1 , .

Boolean earlierHasTarget = earlierInterval.contains ( target );
Boolean laterHasTarget = laterInterval.contains ( target );

Boolean targetContained = ( earlierHasTarget || laterHasTarget );

java.time

Java 8 java.time, , Joda-Time. Joda-Time, . . , java.time Interval.

0

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


All Articles