This question already has an answer here:
How do I compare dates in between in Java?
Example:
date1 is 22-02-2010
date2 is 07-04-2010
today
date3 is 25-12-2010
date3
is always greater than date1
and date2
is always today. How do I verify if today's date is in between date1 and date 3?
Use compareTo:
date1.compareTo(date2);
This code determine today is in some duration.. based on KOREA locale
tl;dr
Or, better, if you add the ThreeTen-Extra library to your project.
Half-open approach, where beginning is inclusive while ending is exclusive.
Bad Choice of Format
By the way, that is a bad choice of format for a text representation of a date or date-time value. Whenever possible, stick with the standard ISO 8601 formats. ISO 8601 formats are unambiguous, understandable across human cultures, and are easy to parse by machine.
For a date-only value, the standard format is YYYY-MM-DD. Note how this format has the benefit of being chronological when sorted alphabetically.
LocalDate
The
LocalDate
class represents a date-only value without time-of-day and without time zone.A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
DateTimeFormatter
As your input strings are non-standard format, we must define a formatting pattern to match.
Use that to parse the input strings.
In date-time work, usually best to define a span of time by the Half-Open approach where the beginning is inclusive while the ending is exclusive. So we want to know if today is the same or later than the start and also before the stop. A briefer way of saying “is the same or later than the start” is “not before the start”.
See the Answer by gstackoverflow showing the list of comparison methods you can call.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
,Calendar
, &SimpleDateFormat
.The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as
Interval
,YearWeek
,YearQuarter
, and more.UPDATE: This “Joda-Time” section below is left intact as history. The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
Joda-Time
Other answers are correct with regard to the bundled java.util.Date and java.util.Calendar classes. But those classes are notoriously troublesome. So here's some example code using the Joda-Time 2.3 library.
If you truly want a date without any time portion and no time zone, then use the
LocalDate
class in Joda-Time. That class provides methods of comparison includingcompareTo
(used with Java Comparators),isBefore
,isAfter
, andisEqual
.Inputs…
Define a formatter describing the input strings…
Use formatter to parse the strings into LocalDate objects…
Dump to console…
When run…
So see if the second is between the other two (exclusively, meaning not equal to either endpoint)…
Working With Spans Of Time
If you are working with spans of time, I suggest exploring in Joda-Time the classes: Duration, Interval, and Period. Methods such as
overlap
andcontains
make comparisons easy.For text representations, look at the ISO 8601 standard’s:
Format: PnYnMnDTnHnMnS
Example: P3Y6M4DT12H30M5S
(Means “three years, six months, four days, twelve hours, thirty minutes, and five seconds”)
Format: start/end
Example: 2007-03-01T13:00:00Z/2008-05-11T15:30:00Z
Joda-Time classes can work with strings in both those formats, both as input (parsing) and output (generating strings).
Joda-Time performs comparisons using the Half-Open approach where the beginning of the span is inclusive while the ending is exclusive. This approach is a wise one for handling spans of time. Search StackOverflow for more info.
Date has before and after methods and can be compared to each other as follows:
For an inclusive comparison:
You could also give Joda-Time a go, but note that:
Back-ports are available for Java 6 and 7 as well as Android.
Following are most common way of comparing dates. But I have prefer first one
Approach-1 : Using Date.before(), Date.after() and Date.equals()
Approach-2 : Date.compareTo()
Approach-3 : Calender.before(), Calender.after() and Calender.equals()
Compare the two dates: