tl;dr
Interval.of( start.toInstant() , stop.toInstant() ).contains( Instant.now() )
Half-Open
In date-time handling, the Half-Open approach is commonly used for defining a span of time. The beginning is inclusive while the ending is exclusive. So a week is defined as starting at the first moment of a Monday and running up to, but not including, the first moment of the following Monday. We sometimes use this Half-Open approach intuitively, where a lunch period of 12 to 1 means start at the stroke of noon but be back at your job or class before the clock strikes 1 PM. Using Half-Open consistently throughout your date-time work will make the logic and programming cleaner, clearer, and simpler.
So your logic should be, “Is now not before beginning AND now is before ending”. Notice that “not before” is a compact way of saying “is equal to OR is after”.
boolean isNowBetweenDateTime = ( ! now.before(startTime) ) && now.before(endTime) ; // Not before start AND is before stop.
ZonedDateTime
The Question and other Answers use troublesome old legacy date-time classes now supplanted by the java.time classes.
The ZonedDateTime
class represents a moment on the timeline with an assigned time zone.
ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime now = ZonedDateTime.now( z );
ZonedDateTime start = now.minusWeeks( 1 ); // Simulating input.
ZonedDateTime stop = now.plusWeeks( 2 ); // Simulating input.
You could write the logic yourself to test if now is between.
Boolean isBetween = ( ! now.isBefore( start ) ) && stop.isBefore( stop );
Interval
If doing more of this kind of work, look at the ThreeTen-Extra project. This library extends the java.time classes with additional functionality. Specifically the Interval
class will be helpful, representing a pair of moments on the timeline. Implements a variety of comparison methods such as contains
, encloses
, abuts
, and overlaps
.
Instantiate a Interval
with a pair of Instant
objects. Instant
represents a moment on the timeline in UTC. We can extract an Instant
object from each ZonedDateTime
object.
Interval interval = Interval.of( start.toInstant() , stop.toInstant() );
Boolean isBetween = interval.contains( now.toInstant() ); // Or pass `Instant.now()`.
You can also get the current moment as an Instant
with a call to Instant.now()
.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date
, .Calendar
, & java.text.SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).
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.