tl;dr
LocalDate
.parse( "2021-01-23" )
.isBefore(
LocalDate.now(
ZoneId.of( "Africa/Tunis" )
)
)
… or:
try
{
org.threeten.extra.LocalDateRange range =
LocalDateRange.of(
LocalDate.of( "2021-01-23" ) ,
LocalDate.of( "2021-02-21" )
)
;
if( range.isAfter(
LocalDate.now( ZoneId.of( "Africa/Tunis" ) )
) { … }
else { … handle today being within or after the range. }
} catch ( java.time.DateTimeException e ) {
// Handle error where end is before start.
}
Details
The other answers ignore the crucial issue of time zone.
The other answers use outmoded classes.
Avoid old date-time classes
The old date-time classes bundled with the earliest versions of Java are poorly designed, confusing, and troublesome. Avoid java.util.Date/.Calendar and related classes.
java.time
LocalDate
For date-only values, without time-of-day and without time zone, use the LocalDate
class.
LocalDate start = LocalDate.of( 2016 , 1 , 1 );
LocalDate stop = start.plusWeeks( 1 );
Time Zone
Be aware that while LocalDate
does not store a time zone, determining a date such as “today” requires a time zone. For any given moment, the date may vary around the world by time zone. For example, a new day dawns earlier in Paris than in Montréal. A moment after midnight in Paris is still “yesterday” in Montréal.
If all you have is an offset-from-UTC, use ZoneOffset
. If you have a full time zone (continent/region), then use ZoneId
. If you want UTC, use the handy constant ZoneOffset.UTC
.
ZoneId zoneId = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( zoneId );
Comparing is easy with isEqual
, isBefore
, and isAfter
methods.
boolean invalidInterval = stop.isBefore( start );
We can check to see if today is contained within this date range. In my logic shown here I use the Half-Open approach where the beginning is inclusive while the ending is exclusive. This approach is common in date-time work. So, for example, a week runs from a Monday going up to but not including the following Monday.
// Is today equal or after start (not before) AND today is before stop.
boolean intervalContainsToday = ( ! today.isBefore( start ) ) && today.isBefore( stop ) ) ;
LocalDateRange
If working extensively with such spans of time, consider adding the ThreeTen-Extra library to your project. This library extends the java.time framework, and is the proving ground for possible additions to java.time.
ThreeTen-Extra includes an LocalDateRange
class with handy methods such as abuts
, contains
, encloses
, overlaps
, and so on.
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.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?