Last active
July 23, 2020 15:24
-
-
Save asdf913/daab84c8b28767738c768b46321ca4bf to your computer and use it in GitHub Desktop.
Java - Check if a particular date within a "java.util.Date" "org.apache.commons.lang3.Range"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.text.DateFormat; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import org.apache.commons.lang3.Range; | |
import org.apache.commons.lang3.time.DateUtils; | |
public class RangeTest { | |
public static void main(final String[] args) throws ParseException { | |
// | |
final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); | |
final Date start = dateFormat.parse("2020-07-21"); | |
final Date end = dateFormat.parse("2020-07-22"); | |
// | |
final Range<Date> range = Range.between(start, end); | |
// | |
if (range != null) { | |
// | |
Date date = null; | |
// | |
for (int i = -1; i <= 2; i++) { | |
System.out.printf("%1$s.contains(%2$s)=%3$s%4$s", range, date = DateUtils.addDays(start, i), | |
range.contains(date), System.lineSeparator()); | |
} // for | |
// | |
} | |
// | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Maven Repository: org.apache.commons » commons-lang3
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.11</version> </dependency>