Beware of Date/Calendar Classes
Caution: The java.util.Date/Calendar classes bundled with Java are notoriously bad. Instead, use the third-party library Joda-Time, or in Java 8 use the new JSR 310 features (inspired by Joda-Time).
Considerations
Think about time as well as date. And time zones. Generally it is best to store and work with UTC time (no time zone offset), then convert to zoned date-time for presentation to user.
Example Code
I'm not promising this source code example is logically consistent, but it will put you in the right direction. Joda-Time 2.3 on Java 7 on a Mac.
The boolean logic in your pseudo-code is flawed, so I ignored that aspect. I focused on the date-time angle.
// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
DateTimeZone denverTimeZone = DateTimeZone.forID( "America/Denver" );
// In real world, get 'bestBeforeDate' from storage. Stored in UTC.
// Call 'withTimeAtStartOfDay' method rather than try to set midnight. Not every day in every time zone has a midnight.
// For this example, we'll hard-code bestBeforeDate to first moment of day Nov 1 in Denver. Then convert to UTC.
DateTime bestBeforeDate = new DateTime( 2013, DateTimeConstants.NOVEMBER, 1, 3, 3, denverTimeZone ).withTimeAtStartOfDay().toDateTime( DateTimeZone.UTC );
DateTime now = new DateTime().toDateTime( DateTimeZone.UTC );
DateTime twoDaysFromNow = now.plusDays( 2 );
if ( bestBeforeDate.isBefore( twoDaysFromNow ) ) {
// Do something
} else {
// Do something else.
}
System.out.println( "bestBeforeDate: " + bestBeforeDate );
System.out.println( "now: " + now );
System.out.println( "twoDaysFromNow: " + twoDaysFromNow );
When run…
bestBeforeDate: 2013-11-01T06:00:00.000Z
now: 2013-12-03T04:54:55.405Z
twoDaysFromNow: 2013-12-05T04:54:55.405Z