JavaJoda time isAfter date解决方法

Java Joda time isAfter date solution

我想比较两个日期。 如果当前日期时间大于或晚于 具体日期,那么它将 return 'True'.

到目前为止我已经试过了。

String deadline = "25/11/2017 11:00:00";
DateTime utc = new DateTime(DateTimeZone.UTC);
DateTimeZone timeZone = DateTimeZone.forID("Asia/Dhaka");
DateTime dhakaTime = utc.toDateTime(timeZone);
//Dead Line Time
DateTimeFormatter format = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
DateTime deadlineTime = format.parseDateTime(deadline.trim());
//Comapare
return deadlineTime.isAfter(dhakaTime.plusDays(2));

因为今天是 23,dhakaTime.plusDays(2) 将是 25,所以它应该 return "true".

但我得到 "false"。

输出值:

dhakaTime.plusDays(2) = 2017-11-25T14:10:27.762+06:00
deadlineTime = 2017-11-25T11:00:00.000+06:00

我是漏了什么还是做错了什么?

它给出 false 正确。

您正在比较

deadLineTime = 2017-11-25T11:00:00.000+06:00 and 
dhakaTime(+2) =  2017-11-25T14:10:27.762+06:00

他们都在同一天,但随着时间的推移,deadLineTime11AMdhakaTime(+2)2PM。所以,

(Nov 25, 2017 11AM)isAfter(Nov 25, 2017 2PM)false.

编辑:测试用例

正如您提到的,您正在测试,以下使用不同的测试用例来比较 deadLinedhakaTime(+1 和 +2 天)。我希望这能让你了解它是如何工作的。

public static void main(String[] args) {
    String deadline = "25/11/2017 11:00:00";
    DateTime utc = new DateTime(DateTimeZone.UTC);
    DateTimeZone timeZone = DateTimeZone.forID("Asia/Dhaka");
    DateTime dhakaTime = utc.toDateTime(timeZone);
    //Dead Line Time
    DateTimeFormatter format = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
    DateTime deadlineTime = format.parseDateTime(deadline.trim());

    System.out.println("Deadline : " + deadline);
    System.out.println("Current datetim : " + dhakaTime);
    System.out.println("current datetime + 1 day : " + dhakaTime.plusDays(1));
    System.out.println("current datetime + 2 day : " + dhakaTime.plusDays(2));

    System.out.println("Is deadline after current datetime:" + deadlineTime.isAfter(dhakaTime)); 
    System.out.println("Is deadline after current datetime + 1 day:" + deadlineTime.isAfter(dhakaTime.plusDays(1))); 
    System.out.println("Is deadline after current datetime + 2 day:" + deadlineTime.isAfter(dhakaTime.plusDays(2))); 
}

尝试使用方法 DateTimeFormatter withZone(DateTimeZone zone) 并使用它来创建 DateTime