user82302124
user82302124

Reputation: 1143

Using DateTime to compare the current date with another date

I'm just starting to look at the JODA library and I want to take the current time and see if it falls within 3 days of a timestamp in the DB.

I'm looking at the example on their API:

public boolean isRentalOverdue(DateTime datetimeRented) {
  Period rentalPeriod = new Period().withDays(2).withHours(12);
  return datetimeRented.plus(rentalPeriod).isBeforeNow();
}

I am taking a String from the database to create a Date object:

DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = (Date)formatter.parse(startDate);

How can I see if Date object in my code falls within 3 days of the current time?:

Period threeDays = new Period().withDays(3);
boolean withinThreeDays = date.plus(threeDays).isBeforeNow();

Upvotes: 1

Views: 1121

Answers (1)

Ilya
Ilya

Reputation: 29693

You can get not parse Joda date from string, and check without SimpleDateFormat and Period

DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");  
DateTime dateTime = dtf.parseDateTime(startDate);  
boolean withinThreeDays = dateTime.plusDays(3).isBeforeNow();

Upvotes: 1

Related Questions