【问题标题】:Comparing date is not working as expected in java比较日期在java中没有按预期工作
【发布时间】:2020-07-06 10:11:10
【问题描述】:

我使用compareTo() 约会。它在以下情况下按预期工作。

  1. 返回 -1 和
  2. 返回 1

但它在这种情况下不起作用 - > 返回零(0)。如果两个日期相等,那么它应该返回零吧?

Actual Date 1 : 2018-04-16T05:30:00.000+0530
Actual Date 2 : 2018-04-16T00:00:00.000+0530

使用compareTo 方法比较日期

Date 1 :2018-04-16
Date 2 :2018-04-16

Java 为此返回 -1。我不知道为什么会这样。我也试过用equals方法。 EX : date1.equals(dat2) -> 返回 FALSE。

注意:这里 Date1 和 Date2 是通过类型转换获得的。我得到的 JSON 日期为1500544580000(未完全给出任意值)。然后我使用 java 方法比较日期。

示例代码

Date date1= (Date) somemethod("Date"); //2018-04-16 Date date2= (Date) somemethod("Date"); //2018-04-16

if (date1.compareTo(date2) < 0 )    syso(false) else
      syso(true)

    

在上面的方法中 true 应该得到打印吧兄弟?但它不工作/打印。我得到了错误因为该 compareTo 方法返回 -1 而不是返回 0。

【问题讨论】:

  • 您能否创建一个简短的小代码示例,在其中创建 2 个日期对象并进行比较?在不知道任何代码的情况下,很难帮助甚至猜测可能发生的事情。
  • 日期和时区相等,但时间不相等 (05:30:00.000 vs 00:00:00.000)。它的行为符合预期
  • 类 Date 表示特定的时间瞬间,精度为毫秒。您的日期具有相同的年、月、日、时区,但小时和分钟不同。它们不相等。
  • @benjaminfrank 当被问及代码示例时,SO 用户通常期望MCVE
  • 立即停止使用Date 它已经过时多年了。从 Java 8 开始,您最好切换到 java.time 包中提供的更新的 Java 日期和时间 API。请参阅 Arvind Kumar Avinash's answer

标签: java json date


【解决方案1】:

我建议您从过时且容易出错的 java.util 日期时间 API 切换到返回正确结果的 modern date-time API,如下所示:

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        // Define format
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSZ");

        // #1
        // Parse the first date-time string to OffsetDateTime
        OffsetDateTime odt1 = OffsetDateTime.parse("2018-04-16T05:30:00.000+0530", formatter);

        // Create an instance of LocalDateTime with the given date, time and zone offset
        LocalDateTime ldt = LocalDateTime.of(LocalDate.of(2018, Month.APRIL, 16), LocalTime.of(5, 30));

        // Get OffsetDateTime from LocalDateTime
        OffsetDateTime odt2 = OffsetDateTime.of(ldt, ZoneOffset.ofHoursMinutes(5, 30));

        // Display the result of comparison
        System.out.println(odt1.compareTo(odt2));

        // #2
        // Parse a date string to LocalDate
        LocalDate ld1 = LocalDate.parse("2018-04-16");

        // Create an instance of LocalDate with year, month and day
        LocalDate ld2 = LocalDate.of(2018, Month.APRIL, 16);

        // Display the result of comparison
        System.out.println(ld1.compareTo(ld2));

        // #3
        // Get Instant from the time-stamp of 1523836800000
        Instant instant = Instant.ofEpochMilli(1523836800000L);

        // Get OffsetDateTime from instant
        OffsetDateTime odt = instant.atOffset(ZoneOffset.ofHoursMinutes(5, 30));

        // Display the result of comparison
        System.out.println(odt.compareTo(odt2));

        // #4
        // Parse the second date-time string to OffsetDateTime
        OffsetDateTime odt3 = OffsetDateTime.parse("2018-04-16T00:00:00.000+0530", formatter);

        // Get the LocalDate from the first date-time string
        LocalDate localDate1 = odt1.toLocalDate();

        // Get the LocalDate from the second date-time string
        LocalDate localDate3 = odt3.toLocalDate();

        // Display the result of comparison
        System.out.println(localDate1.compareTo(localDate3));
    }
}

输出:

0
0
0
0

【讨论】:

  • 虽然这些示例是正确的,但这并不能解决 OP(无法解决)问题,因为他期望 2 个不同的日期时间相等
  • @jhamon - OP 提到了Note : Here Date1 and Date2 is getting by typecasting. I am getting the JSON date as 1500544580000 (Arbitrary value not exactly given). And then i am comparing the date using java method.,他在下面给出了一个示例代码。问题不清楚。因此,我提出了三个可能有助于 OP 继续进行的选项。我一直在等待他/她的评论,我将根据这些评论发布所需的更新。
  • 增加了一个选项 (#4)。如果解决方案解决了他/她的问题,仍在等待 OP 的评论/确认。
猜你喜欢
  • 2019-11-27
  • 2013-01-31
  • 1970-01-01
  • 2018-01-21
  • 1970-01-01
  • 2020-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多