Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check a Date is between two dates in Java [duplicate]

Tags:

java

date

One thing I want to know is how to calculate what date will it be 10 days from today.

Second thing is to check if one Date is between two other Dates. For example, let's say I have an app that shows what events I need to do in the next 10 days (planner). Now how can I see if the date I assigned to an event is between today and the date that is 10 days from today?

like image 527
dimitar Avatar asked May 24 '10 10:05

dimitar


2 Answers

Manipulating and comparing dates using java.util.Date and java.util.Calendar is pretty a pain, that's why JodaTime exist. None of the answers as far have covered the time in question. The comparisons may fail when the dates have a non-zero time. It's also unclear whether you want an inclusive or exclusive comparison. Most of the answers posted so far suggest exclusive comparision (i.e. May 24 is not between May 20 and May 24) while in real it would make more sense to make it inclusive (i.e. May 24 is between May 20 and May 24).


One thing I want to know is how to calculate what date will it be 10 days from today.

With the standard Java SE 6 API, you need java.util.Calendar for this.

Calendar plus10days = Calendar.getInstance();
plus10days.add(Calendar.DAY_OF_YEAR, 10);

With JodaTime you would do like this:

DateTime plus10days = new DateTime().plusDays(10);

Second thing is to check if one Date is between two other Dates. For example, let's say I have an app that shows what events I need to do in the next 10 days (planner). Now how can I see if the date I assigned to an event is between today and the date that is 10 days from today?

Now comes the terrible part with Calendar. Let's prepare first:

Calendar now = Calendar.getInstance();
Calendar plus10days = Calendar.getInstance();
plus10days.add(Calendar.DAY_OF_YEAR, 10);
Calendar event = Calendar.getInstance();
event.set(year, month - 1, day); // Or setTime(date);

To compare reliably using Calendar#before() and Calendar#after(), we need to get rid of the time first. Imagine it's currently 24 May 2010 at 9.00 AM and that the event's date is set to 24 May 2010 without time. When you want inclusive comparison, you would like to make it return true at the same day. I.e. both the (event.equals(now) || event.after(now)) or -shorter but equally- (!event.before(now)) should return true. But actually none does that due to the presence of the time in now. You need to clear the time in all calendar instances first like follows:

calendar.clear(Calendar.HOUR);
calendar.clear(Calendar.HOUR_OF_DAY);
calendar.clear(Calendar.MINUTE);
calendar.clear(Calendar.SECOND);
calendar.clear(Calendar.MILLISECOND);

Alternatively you can also compare on day/month/year only.

if (event.get(Calendar.YEAR) >= now.get(Calendar.YEAR)
    && event.get(Calendar.MONTH) >= now.get(Calendar.MONTH)
    && event.get(Calendar.DAY_OF_MONTH) >= now.get(Calendar.DAY_OF_MONTH)
{
    // event is equal or after today.
}

Very verbose all.

With JodaTime you can just use DateTime#toLocalDate() to get the date part only:

LocalDate now = new DateTime().toLocalDate();
LocalDate plus10days = now.plusDays(10);
LocalDate event = new DateTime(year, month, day, 0, 0, 0, 0).toLocalDate();
if (!event.isBefore(now) && !event.isAfter(plus10days)) {
    // Event is between now and 10 days (inclusive).
}

Yes, the above is really all you need to do.

like image 139
BalusC Avatar answered Oct 04 '22 16:10

BalusC


public static boolean between(Date date, Date dateStart, Date dateEnd) {
    if (date != null && dateStart != null && dateEnd != null) {
        if (date.after(dateStart) && date.before(dateEnd)) {
            return true;
        }
        else {
            return false;
        }
    }
    return false;
}

EDIT: Another suggested variant:

public Boolean checkDate(Date startDate, Date endDate, Date checkDate) { 
    Interval interval = new Interval(new DateTime(startDate), 
                                     new DateTime(endDate));   
    return interval.contains(new DateTime(checkDate)); 
}
like image 27
Krish Lakshmanan Avatar answered Oct 04 '22 15:10

Krish Lakshmanan