Java: how do I check if a Date is within a certain range?

Mike Sickler :

I have a series of ranges with start dates and end dates. I want to check to see if a date is within that range.

Date.before() and Date.after() seem to be a little awkward to use. What I really need is something like this pseudocode:

boolean isWithinRange(Date testDate) {
    return testDate >= startDate && testDate <= endDate;
}

Not sure if it's relevant, but the dates I'm pulling from the database have timestamps.

Paul Tomblin :
boolean isWithinRange(Date testDate) {
   return !(testDate.before(startDate) || testDate.after(endDate));
}

Doesn't seem that awkward to me. Note that I wrote it that way instead of

return testDate.after(startDate) && testDate.before(endDate);

so it would work even if testDate was exactly equal to one of the end cases.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to check if a date is within a certain range?

Java - How do I allow users to select a date range and then check that the date range is either 7 or 14 days?

How do I find how many days are within a date range that are within another date range in PHP?

How to check if the date is within a specifed date range

How do I check that the value of a param is a number in a certain range

How to do I exclude a specific date range within a specific period?

How to check if a time is within a range with date

Ruby: How to check if date and time are within a range

How to check if a selected date range is within a date range

How do I check if a certain date has passed in C#?

How can I count distinct values of certain attributes within a date range?

How to check if all numbers in an object are within a certain range?

How do I generate random numbers but not repeating and within a certain range based on user input?

How do I conditionally highlight dates within a start and end date range based on individual date rows?

Check whether dates are within the date range in selenium web driver ( Java)

How can i check if date is on range on Python?

Javascript Validation - How do I check that the number entered into a form is between a certain range?

How do i check a date value falls with in a date range last month

How do I ensure a number is within a range?

How do I extract dates within a range?

How do I determine if a value is within a range?

How to check if a date is within a given range in Microsoft excel

How do I read a certain date range from a partitioned parquet file in Spark

How can I print unicode symbols within a certain range?

How do I check if a given hour and minute (int's) falls within or is equal to any range within a given set of ranges of times?

How do I check if a given hour and minute (int's) falls within or is equal to any range within a given set of ranges of times?

How do I check date validity in MM/dd/yyyy in Java?

How do I check for an element with a certain class?

How do I check for an object at a certain position?