Domanda

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.

È stato utile?

Soluzione

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() );

Altri suggerimenti

The actual value a.compareTo(b) returns is meaningless. The only thing you can trust is that if it's positive a is "larger" than b, and if it's negative, a is "smaller". You can't count on its absolute value to determine the difference between the two.

You could, however, just compare the unix time representation of both dates:

TimeStamp due_date = ...;
long dueDateMillis = due_date.getTime(); 
long t = System.currTimeMillis();
long threshold = 24L * 60L * 60L * 1000L; // One day in milliseconds

if (dueDateMillis - t <= threshold) {
    // Renew book
}

Another way to do this is using the Calendar object:

Calendar today = Calendar.getInstance();
today.setTimeInMillis(System.currentTimeMillis()); // time today

Timestamp dueDateTs = new Timestamp(...);
Calendar dueDate = Calendar.getInstance();
dueDate.setTimeInMillis(dueDateTs.getTime());
dueDate.roll(Calendar.DAY_OF_YEAR, false); // to subtract 1 day

if(today.after(dueDate)) {
// do your magic
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top