1 day less then due_date
too early to renew
Timestamp t = new Timestamp(new Date().getTime());
You should not ignore the issue of time zone. Omitting time zone means your JVM's (host computer's) default time zone will apply. Your results will vary.
The definition of a "day" and "yesterday" depends on your particular time zone.
Use a proper time zone name (mostly continent slash city). Avoid the 3 or 4 letter codes as they are neither standardized nor unique.
Joda-Time offers the Interval class to define a span of time. In your case the span is two days, the due date's day plus the day before. (By the way, both your posted questions and your programming will improve if you work harder at focusing and simplifying your problem as I just did in that preceding sentence.)
Usually in date-time work we use the "half-open" approach to define a span. That means the beginning is inclusive and the ending in exclusive for purposes of comparison. So for your purpose we want to run from the
first moment of the day before due date up to, but not including, the
first moment of the day *after* due date.
Your input string is nearly in ISO 8601 standard format. Just replace the SPACE with a
T. Joda-Time has built-in parsers for ISO 8601 formats.
Example code in Joda-Time 2.3.
String inputDueDateRaw = "2014-05-09 11:36:41.816" String inputDueDate = inputDueDateRaw.replace( " ", "T" ); DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" ); DateTime due = new DateTime( inputDueDate, timeZone ); // Note the time zone by which to interpret the parsing of the string. DateTime dayBeforeDue = due.withTimeAtStartOfDay().minusDays( 1 ).withTimeAtStartOfDay(); DateTime dayAfterDue = due.withTimeAtStartOfDay().plusDays( 1 ).withTimeAtStartOfDay(); // Half-open. Up to but not including day after. Interval renewalInterval = new Interval( dayBeforeDue, dayAfterDue );
Test if the current moment is within that interval, using half-open approach to comparison.
boolean isNowEligibleForRenewal = renewalInterval.contains( DateTime.now() );