This and that of the implementation of date judgment within the period in Java

What is this?

Theme

I want to determine whether a date is included or not included for a given two periods (dates). For example, for July 20th to July 30th, "August 1st is not included" and "July 20th, July 25th, July 30th" are included. If you compare it with a line ...

---●---------------------●------
   7/20                 7/30

● will mean that the day is included.

How to implement

Part 1

boolean between(LocalDate date){
  return (startDate.isBefore(date) && endDate.isAfter(date)) || startDate.equals(date) || endDate.equals(date);
}

Part 2 (smarter)

boolean between(LocalDate date){
  return !(startDate.isAfter(date) || endDate.isBefore(date));
}

Extra edition

---○---------------------○------
   7/20                 7/30

○ means that the day is not included.

Part 1

boolean between(LocalDate date){
  return (startDate.isBefore(date) && endDate.isAfter(date));
}

boolean between(LocalDate date){
  //It shouldn't be based on external values, but it's simple
  return date.isAfter(startDate) && date.isBefore(endDate);
}

Part 2 (smarter?)

boolean between(LocalDate date){
  return startDate.compareTo(date) * endDate.compareTo(date) < 0;
}

boolean between(LocalDate date){
  // //It should not be based on external values, but as a pattern
  return startDate.compareTo(date) * date.compareTo(endDate) > 0;
}

Impressions

Recommended Posts

This and that of the implementation of date judgment within the period in Java
This and that of the JDK
[Java] Where is the implementation class of annotation that exists in Bean Validation?
This and that for editing ini in Java. : inieditor-java
Implementation of gzip in java
Implementation of tri-tree in Java
A program (Java) that outputs the sum of odd and even numbers in an array
Shout Java at the heart of technology-Themes and elemental technologies that Java engineers should pursue in 2017-
[Java] Judgment of identity and equivalence
This and that of Core Graphics
This and that about Base64 (Java)
Implementation of like function in Java
This and that of exclusive control
Mechanism and characteristics of Collection implementation class often used in Java
Let's implement the condition that the circumference and the inside of the Ougi shape are included in Java [Part 2]
Let's implement the condition that the circumference and the inside of the Ougi shape are included in Java [Part 1]
This and that of Swift corner Radius
Implementation of DBlayer in Java (RDB, MySQL)
Get the result of POST in Java
How to get the date in java
The story of writing Java in Emacs
Discrimination of Enums in Java 7 and above
A collection of phrases that impresses the "different feeling" of Java and JavaScript
The story of forgetting to close a file in Java and failing
Comparison of thread implementation methods in Java and lambda expression description method
Sample program that returns the hash value of a file in Java
Find the maximum and minimum of the five numbers you entered in Java
Review of "strange Java" and Java knowledge that is often forgotten in Java Bronze
Regarding the transient modifier and serialization in Java
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
I received the data of the journey (diary application) in Java and visualized it # 001
The story of making ordinary Othello in Java
About the idea of anonymous classes in Java
The story of learning Java in the first programming
Measure the size of a folder in Java
[Java] The confusing part of String and StringBuilder
I compared the characteristics of Java and .NET
Feel the passage of time even in Java
Basics of threads and Callable in Java [Beginner]
This and that of conditional branching of rails development
Java unit test Judgment of specified day of the week Determines whether ArDate, Date, Calendar, and long are specified days of the week.
[Java] Is it unnecessary to check "identity" in the implementation of the equals () method?
Code that deletes all files of the specified prefix in AWS S3 (Java)
[Java] Judgment by entering characters in the terminal
The date time of java8 has been updated
Import files of the same hierarchy in Java
Parse the date and time string formatted by the C asctime function in Java
[Java] What to do if the contents saved in the DB and the name of the enum are different in the enum that reflects the DB definition
[Android 9.0 Pie Java] Implement setOnTouchListener in the margin of RecyclerView and close the soft keyboard
Error logging and exception handling that you can't often see in the Java area
Find the maximum and minimum values out of the 5 numbers entered in Java (correction ver)
Get the URL of the HTTP redirect destination in Java
In Time.strptime,% j (total date of the year) is
Write ABNF in Java and pass the email address
Please note the division (division) of java kotlin Int and Int
[For beginners] DI ~ The basics of DI and DI in Spring ~
The comparison of enums is ==, and equals is good [Java]
Organizing the current state of Java and considering the future
Java language from the perspective of Kotlin and C #
[Java] Get the file in the jar regardless of the environment