欢迎访问宙启技术站
智能推送

Java函数:如何实现日期时间的比较函数?

发布时间:2023-06-18 09:51:56

在Java中实现日期时间比较可以通过使用Java.util.Date类或Java.time包中的LocalDateTime类等。下面分别介绍这两个类的比较方法。

一、Java.util.Date类比较方法

Java.util.Date类提供了以下用于比较日期的方法:

1. before(Date when):如果当前日期早于when日期,则返回true,否则返回false。

2. after(Date when):如果当前日期晚于when日期,则返回true,否则返回false。

3. equals(Object obj):如果当前日期等于obj,则返回true,否则返回false。

4. compareTo(Date anotherDate):比较两个Date类型的日期的先后顺序,返回值为0表示相等,返回大于0的值表示当前日期晚于anotherDate,返回小于0的值表示当前日期早于anotherDate。

示例代码如下:

import java.util.Date;

public class DateCompareTest {
    public static void main(String[] args) {
        Date date1 = new Date();
        Date date2 = new Date(System.currentTimeMillis() + 60000L);
        // before()方法
        if (date1.before(date2)) {
            System.out.println(date1 + " 早于 " + date2);
        }
        // after()方法
        if (date2.after(date1)) {
            System.out.println(date2 + " 晚于 " + date1);
        }
        // equals()方法
        if (date1.equals(date2)) {
            System.out.println(date1 + " 等于 " + date2);
        } else {
            System.out.println(date1 + " 不等于 " + date2);
        }
        // compareTo()方法
        int result = date1.compareTo(date2);
        if (result == 0) {
            System.out.println(date1 + " 等于 " + date2);
        } else if (result > 0) {
            System.out.println(date1 + " 晚于 " + date2);
        } else {
            System.out.println(date1 + " 早于 " + date2);
        }
    }
}

输出结果如下:

Sat Oct 16 20:06:07 CST 2021 早于 Sat Oct 16 20:07:07 CST 2021
Sat Oct 16 20:07:07 CST 2021 晚于 Sat Oct 16 20:06:07 CST 2021
Sat Oct 16 20:06:07 CST 2021 不等于 Sat Oct 16 20:07:07 CST 2021
Sat Oct 16 20:06:07 CST 2021 早于 Sat Oct 16 20:07:07 CST 2021

二、Java.time包中LocalDateTime类比较方法

Java.time包中的LocalDateTime类提供了以下用于比较日期的方法:

1. isBefore(LocalDateTime other):如果当前日期早于other日期,则返回true,否则返回false。

2. isAfter(LocalDateTime other):如果当前日期晚于other日期,则返回true,否则返回false。

3. isEqual(LocalDateTime other):如果当前日期等于other日期,则返回true,否则返回false。

4. compareTo(LocalDateTime other):比较两个LocalDateTime类型的日期的先后顺序,返回值为0表示相等,返回大于0的值表示当前日期晚于other,返回小于0的值表示当前日期早于other。

示例代码如下:

import java.time.LocalDateTime;

public class LocalDateTimeCompareTest {
    public static void main(String[] args) {
        LocalDateTime localDateTime1 = LocalDateTime.now();
        LocalDateTime localDateTime2 = LocalDateTime.now().plusMinutes(1);
        // isBefore()方法
        if (localDateTime1.isBefore(localDateTime2)) {
            System.out.println(localDateTime1 + " 早于 " + localDateTime2);
        }
        // isAfter()方法
        if (localDateTime2.isAfter(localDateTime1)) {
            System.out.println(localDateTime2 + " 晚于 " + localDateTime1);
        }
        // isEqual()方法
        if (localDateTime1.isEqual(localDateTime2)) {
            System.out.println(localDateTime1 + " 等于 " + localDateTime2);
        } else {
            System.out.println(localDateTime1 + " 不等于 " + localDateTime2);
        }
        // compareTo()方法
        int result = localDateTime1.compareTo(localDateTime2);
        if (result == 0) {
            System.out.println(localDateTime1 + " 等于 " + localDateTime2);
        } else if (result > 0) {
            System.out.println(localDateTime1 + " 晚于 " + localDateTime2);
        } else {
            System.out.println(localDateTime1 + " 早于 " + localDateTime2);
        }
    }
}

输出结果如下:

2021-10-16T20:22:21.310373 早于 2021-10-16T20:23:21.311111
2021-10-16T20:23:21.311111 晚于 2021-10-16T20:22:21.310373
2021-10-16T20:22:21.310373 不等于 2021-10-16T20:23:21.311111
2021-10-16T20:22:21.310373 早于 2021-10-16T20:23:21.311111

总结:

通过上述两种方法可以实现Java中日期时间的比较,其中Java.util.Date类和Java.time包中的LocalDateTime类提供了不同的方法来进行比较,使用时需要根据具体场景选择合适的方法。同时需要注意的是,在进行比较时需要保证日期时间格式的一致性,尤其是在使用字符串转换为日期时间时需要特别注意,否则比较结果可能会出现错误。