IsAfter isBefore Java 8 LocalDateTime One Day Different

I want to compare two dates with today's date. Does isAfter and better for this? isAfter and isBefore cannot detect one-day changes. Let's say:

If today is 20 Nov. I put in range 20 Nov-21 Nov.

if(todayDate.isAfter(startDate) && todayDate.isBefore(endDate))
{
  // task
}

This code will not detect that it is in range today. OR / || not applicable because I have a set of ranges for testing. Any idea on this?

+4
source share
1 answer

This will solve this problem: just add a check to see if a start or end date is set today, which you will need to implement yourself.

if( (todayDate.isAfter(startDate) && todayDate.isBefore(endDate) ) || (todayDate.isEqual(startDate) || todayDate.isEqual(endDate) )
{
     // task
}

This is because, isAfterand isBeforeare strict.

: :

if(!todayDate.isAfter(endDate) && !todayDate.isBefore(startDate))
{
    // task
}

isAfter, endDate .

isBefore, startDate .

+6

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


All Articles