Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine if a date is between two dates in Java? [duplicate]

Tags:

java

date

How can I check if a date is between two other dates, in the case where all three dates are represented by instances of java.util.Date?

like image 748
Gnaniyar Zubair Avatar asked May 19 '09 14:05

Gnaniyar Zubair


People also ask

How do you validate a date is in between two dates in Java?

We can use the simple isBefore , isAfter and isEqual to check if a date is within a certain date range; for example, the below program check if a LocalDate is within the January of 2020. startDate : 2020-01-01 endDate : 2020-01-31 testDate : 2020-01-01 testDate is within the date range.

How do you check if a date is in a given range in Java?

A Java example to check if a provided date is within a range of 3 momths before and after current date. The idea is quite simple, just use Calendar class to roll the month back and forward to create a “date range”, and use the Date. before() and Date. after() to check if the Date is within the range.

Which method finds the difference in days between two dates?

Use the strptime(date_str, format) function to convert a date string into a datetime object as per the corresponding format . To get the difference between two dates, subtract date2 from date1.


1 Answers

This might be a bit more readable:

Date min, max;   // assume these are set to something Date d;          // the date in question  return d.after(min) && d.before(max); 
like image 135
Nathan Feger Avatar answered Sep 17 '22 18:09

Nathan Feger