tl;dr
Interval.of(
Instant.ofEpochMilli( 1_412_801_340_000L ) , // 2014-10-08T20:49:00Z
Instant.ofEpochMilli( 1_412_808_540_000L ) // 2014-10-08T22:49:00Z
).contains(
Instant.ofEpochMilli( 1_447_195_740_000L ) // 2015-11-10T22:49:00Z
)
false
java.time
If you wish to consider those values as date-times in UTC, then the modern approach uses the java.time classes that supplanted the troublesome old Date
& Calendar
classes.
Instant
The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).
Sample data errors/ambiguity
By the way, your date-time description of your sample count-of-milliseconds-since-start-of-1970-UTC are incorrect. See my output below. You seem to have an offset affecting your numbers, an offset of 4 hours in the first two and then 5 hours in the last one, probably due to to a "fall-back" change in Daylight Saving Time (DST). This demonstrates why tracking time as a count-from-epoch is such a poor idea, fraught with problems of ambiguity and uncaught errors. I suggest always serializing such moments to text using standard ISO 8601 formats.
Instant start = Instant.ofEpochMilli( 1_412_801_340_000L ); // 2014-10-08T20:49:00Z (not 10/8/14 4:49 PM in UTC time)
Instant stop = Instant.ofEpochMilli( 1_412_808_540_000L ); // 2014-10-08T22:49:00Z (not 10/8/14 6:49 PM in UTC time)
Instant test = Instant.ofEpochMilli( 1_447_195_740_000L ); // 2015-11-10T22:49:00Z (not 11/10/15 5:49 PM in UTC time)
See that code run live at IdeOne.com.
start.toString(): 2014-10-08T20:49:00Z
stop.toString(): 2014-10-08T22:49:00Z
test.toString(): 2015-11-10T22:49:00Z
Compare
Compare Instant
objects with isBefore
, isAfter
, and equals
.
Generally the best approach to handling spans of time is known as Half-Open, where the beginning is inclusive and the ending is exclusive. So, for example, a day starts at the first moment of the day and runs up to, but does not include, the first moment of the following day.
boolean isTestWithinRange =
( test.equals( start ) || test.isAfter( start ) )
&&
test.isBefore( stop )
;
A shorter way of asking "is equal to or later than" is "is not before".
boolean isTestWithinRange =
( ! test.isBefore( start ) )
&&
test.isBefore( stop )
;
Even shorter, use the org.threeten.extra.Interval
class found in the ThreeTen-Extra project, a library you can add to your Java project. Not sure if this yet works in Android.
Interval interval = Interval.of( start , stop ) ;
boolean isTestWithinRange = interval.contains( test ) ;
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.
With a JDBC driver complying with JDBC 4.2 or later, you may exchange java.time objects directly with your database. No need for strings or java.sql.* classes.
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.