1

我刚开始查看 JODA 库,我想查看当前时间,看看它是否在数据库中时间戳的 3 天内。

我正在查看他们 API 上的示例:

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

我从数据库中获取一个字符串来创建一个日期对象:

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

如何查看我的代码中的 Date 对象是否在当前时间的 3 天内?:

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

1 回答 1

1

您可以不从字符串中解析 Joda 日期,并在没有SimpleDateFormat和的情况下进行检查Period

DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");  
DateTime dateTime = dtf.parseDateTime(startDate);  
boolean withinThreeDays = dateTime.plusDays(3).isBeforeNow();
于 2012-09-09T17:46:54.930 回答