user3145373 ツ user3145373 ツ - 3 years ago 161
Java Question

Date comparison confusion

I have

due_date
=
2014-05-09 11:36:41.816
.

I want to check condition that if today date is same as
due_date
or
1 day less then due_date
then user can
renew
other wise have to show message that
too early to renew
.
means if I renew on
date 8
then user can do but if user do it on
date 7
then he is not allowed and display message.

I know that to check for same day means
date 9
, i can use :

Timestamp t = new Timestamp(new Date().getTime());

if (t.compareTo(due_date)==0){
//renew book
}


but i don't know that how to do for 1 day before calculation.
So any guidance to do for that.

Answer Source

Decent Date-Time Library

You should be using either Joda-Time or the new java.time in Java 8, as the old java.util.Date and .Calendar classes are notoriously troublesome.

Time Zone

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.

If your input string has no time zone offset, meaning it is in UTC, then specify using the built-in constant DateTimeZone.UTC.

Interval

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.)

Half-Open

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.

ISO 8601

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

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() );
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download