0

我有due_date= 2014-05-09 11:36:41.816

我想检查条件,如果今天的日期相同,due_date或者1 day less then due_date用户可以renew其他方式必须显示消息too early to renew。意味着如果我续订,date 8则用户可以执行,但如果用户续订,则date 7不允许他执行并显示消息。

我知道要检查同一天意味着date 9,我可以使用:

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

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

但我不知道在计算前 1 天该怎么做。因此,为此提供任何指导。

4

3 回答 3

2

体面的日期时间库

您应该在 Java 8 中使用Joda-Time或新的java.time,因为旧的 java.util.Date 和 .Calendar 类是出了名的麻烦。

时区

你不应该忽视时区的问题。省略时区意味着您的 JVM(主机)的默认时区将适用。你的结果会有所不同。

“一天”和“昨天”的定义取决于您的特定时区。

使用适当的时区名称(主要是大陆斜线城市)。避免使用 3 或 4 个字母代码,因为它们既不标准化也不独特。

如果您的输入字符串没有时区偏移量,即它是UTC格式,则使用内置常量指定DateTimeZone.UTC

间隔

Joda-Time 提供了Interval类来定义时间跨度。在您的情况下,跨度是两天,到期日加上前一天。(顺便说一句,如果您像我在前一句中所做的那样更加努力地集中精力和简化您的问题,那么您发布的问题和您的编程都会得到改善。)

半开

通常在日期时间工作中,我们使用“半开”方法来定义跨度。这意味着出于比较的目的,开头是包容性的,而结尾是排他性的。因此,出于您的目的,我们希望从first moment of the day before due date最高(但不包括)运行first moment of the day *after* due date.

ISO 8601

您的输入字符串几乎采用ISO 8601标准格式。只需将 SPACE 替换为T. Joda-Time 有内置的 ISO 8601 格式解析器。

示例代码

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

测试当前时刻是否在该区间内,使用半开方法进行比较。

boolean isNowEligibleForRenewal = renewalInterval.contains( DateTime.now() );
于 2014-05-09T05:46:51.813 回答
1

返回的实际值a.compareTo(b)是没有意义的。您唯一可以相信的是,如果它是正数,a则“大于” b,如果是负数,a则“小于”。你不能指望它的绝对值来确定两者之间的差异。

但是,您可以只比较两个日期的 unix 时间表示:

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
}
于 2014-05-08T06:42:27.280 回答
0

另一种方法是使用 Calendar 对象:

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
}
于 2014-05-08T07:27:47.740 回答