Database
If you are using a good database with sophisticated date-time handling, such as Postgres, the database server may be able to help you by performing queries. Perhaps you are not using such a database, as you mention that date and time are separate fields. In a sophisticated database those would be a single date-time type.
Time Zone
Your question fails to address the issue of time zones. Generally best to specify a time zone rather than rely on defaults. For my example code below, I arbitrarily chose Kolkata India (formerly Calcutta) time zone with an offset of 5:30 ahead of UTC/GMT.
Use Joda-Time
If you want to do the work in Java, you definitely should avoid using the bundled java.util.Date/Calendar classes. Either use the Joda-Time library, or in Java 8, the new java.time.* classes (inspired by Joda-Time).
Joda-Time offers classes with handy methods for your very purpose: Interval, Duration, and Period. Java 8's java.time.* offers similar.
The Interval
class holds a span of time defined by a pair of start-stop DateTime
objects. The class offers a overlaps
method to tell you if one interval coincides with another.
Example code
// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;
DateTimeZone timeZone = DateTimeZone.forID( "Asia/Kolkata" );
Interval i1 = new Interval( new DateTime( "2013-12-10T10:00:00", timeZone ), new DateTime( "2013-12-12T15:00:00", timeZone ) );
Interval i2 = new Interval( new DateTime( "2013-12-16T09:00:00", timeZone ), new DateTime( "2013-12-16T12:00:00", timeZone ) );
Interval i3 = new Interval( new DateTime( "2013-12-16T12:15:00", timeZone ), new DateTime( "2013-12-16T16:00:00", timeZone ) );
Interval test1 = new Interval( new DateTime( "2011-01-01T10:00:00", timeZone ), new DateTime( "2011-01-07T10:00:00", timeZone ) ); // Note the year: 2011.
Interval test2 = new Interval( new DateTime( "2013-12-10T11:00:00", timeZone ), new DateTime( "2013-12-10T13:00:00", timeZone ) );
Dump to console…
System.out.println( "i1: " + i1 );
System.out.println( "i2: " + i2 );
System.out.println( "i3: " + i3 );
System.out.println( "test1: " + test1 );
System.out.println( "test2: " + test2 );
System.out.println( "test1 overlaps i1: " + test1.overlaps( i1 ) );
System.out.println( "test2 overlaps i1: " + test2.overlaps( i1 ) );
When run…
i1: 2013-12-10T10:00:00.000+05:30/2013-12-12T15:00:00.000+05:30
i2: 2013-12-16T09:00:00.000+05:30/2013-12-16T12:00:00.000+05:30
i3: 2013-12-16T12:15:00.000+05:30/2013-12-16T16:00:00.000+05:30
test1: 2011-01-01T10:00:00.000+05:30/2011-01-07T10:00:00.000+05:30
test2: 2013-12-10T11:00:00.000+05:30/2013-12-10T13:00:00.000+05:30
test1 overlaps i1: false
test2 overlaps i1: true
Use UTC
While my example uses a specific time zone, generally it is best if you use UTC/GMT (no time zone offset) in your business logic and storage/database. Translate to a time zone only for presentation to the user.
Converting is easy by specifying the built-in time zone constant: DateTimeZone.UTC
DateTime dateTimeInUtc = new DateTime( DateTimeZone.UTC );
// - or -
DateTime nowInKolkata = new DateTime( DateTimeZone.forID( "Asia/Kolkata" );
DateTime dateTimeConvertedToUtc = nowInKolkata.toDateTime( DateTimeZone.UTC );