如何在 Java 中比较日期和时间

x33g5p2x  于9个月前 转载在 Java  
字(5.0k)|赞(0)|评价(0)|浏览(132)

在本文中,您将学习在 Java 中比较不同时区中的日期、时间、日期时间和日期时间的几种方法。

我们使用 Java 8 中引入的 DateTime API(如 LocalDate、LocalTime、LocalDateTime、ZonedDateTime)以及较旧的类(如 Date 和 Calendar)来演示如何比较日期。

使用 LocalDate 在 Java 中比较日期

import java.time.LocalDate;

public class CompareLocalDateExample {
    public static void main(String[] args) {
        LocalDate date1 = LocalDate.now();
        LocalDate date2 = LocalDate.of(2020, 1, 31);

        // isAfter() method
        if(date1.isAfter(date2)) {
            System.out.println(date1 + " is after " + date2);
        }

        // isBefore() method
        if(date1.isBefore(date2)) {
            System.out.println(date1 + " is before " + date2);
        }

        // isEqual() method
        if(date1.isEqual(date2)) {
            System.out.println(date1 + " is equal to " + date2);
        }

        // compareTo() method
        int diff = date1.compareTo(date2);
        if(diff > 0) {
            System.out.println(date1 + " is greater than " + date2);
        } else if (diff < 0) {
            System.out.println(date1 + " is less than " + date2);
        } else {
            System.out.println(date1 + " is equal to " + date2);
        }
    }
}

使用 LocalDateTime 比较 Java 中的日期和时间

import java.time.LocalDateTime;

public class CompareLocalDateTimeExample {
    public static void main(String[] args) {
        LocalDateTime date1 = LocalDateTime.now();
        LocalDateTime date2 = LocalDateTime.of(2020, 1, 31, 10, 15, 45);

        // isAfter() method
        if(date1.isAfter(date2)) {
            System.out.println(date1 + " is after " + date2);
        }

        // isBefore() method
        if(date1.isBefore(date2)) {
            System.out.println(date1 + " is before " + date2);
        }

        // isEqual() method
        if(date1.isEqual(date2)) {
            System.out.println(date1 + " is equal to " + date2);
        }

        // compareTo() method
        int diff = date1.compareTo(date2);
        if(diff > 0) {
            System.out.println(date1 + " is greater than " + date2);
        } else if (diff < 0) {
            System.out.println(date1 + " is less than " + date2);
        } else {
            System.out.println(date1 + " is equal to " + date2);
        }
    }
}

使用 ZonedDateTime 比较 Java 中不同时区的日期和时间

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class CompareZonedDateTimeExample {
    public static void main(String[] args) {
        ZonedDateTime date1 = ZonedDateTime.of(
                LocalDateTime.of(2020, 1, 31, 10, 30, 45),
                ZoneId.of("America/New_York"));
        ZonedDateTime date2 = ZonedDateTime.of(
                LocalDateTime.of(2020, 1, 31, 10, 30, 45),
                ZoneId.of("Europe/Paris"));

        // isAfter() method
        if(date1.isAfter(date2)) {
            System.out.println(date1 + " is after " + date2);
        }

        // isBefore() method
        if(date1.isBefore(date2)) {
            System.out.println(date1 + " is before " + date2);
        }

        // isEqual() method
        if(date1.isEqual(date2)) {
            System.out.println(date1 + " is equal to " + date2);
        }

        // compareTo() method
        int diff = date1.compareTo(date2);
        if(diff > 0) {
            System.out.println(date1 + " is greater than " + date2);
        } else if (diff < 0) {
            System.out.println(date1 + " is less than " + date2);
        } else {
            System.out.println(date1 + " is equal to " + date2);
        }
    }
}

使用 LocalTime 比较 Java 中的时间

import java.time.LocalTime;

public class CompareLocalTimeExample {
    public static void main(String[] args) {
        LocalTime time1 = LocalTime.of(15, 20, 40);
        LocalTime time2 = LocalTime.of(10, 30, 50);

        // isAfter() method
        if(time1.isAfter(time2)) {
            System.out.println(time1 + " is after " + time2);
        }

        // isBefore() method
        if(time1.isBefore(time2)) {
            System.out.println(time1 + " is before " + time2);
        }

        // compareTo() method
        int diff = time1.compareTo(time2);
        if(diff > 0) {
            System.out.println(time1 + " is greater than " + time2);
        } else if (diff < 0) {
            System.out.println(time1 + " is less than " + time2);
        } else {
            System.out.println(time1 + " is equal to " + time2);
        }
    }
}

使用 Date 类比较日期

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class CompareDateExample {
    public static void main(String[] args) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date date1 = sdf.parse("2020-01-31 10:30:45");
        Date date2 = sdf.parse("2020-10-10 14:20:00");

        // after() method
        if(date1.after(date2)) {
            System.out.println(date1 + " is after " + date2);
        }

        // before() method
        if(date1.before(date2)) {
            System.out.println(date1 + " is before " + date2);
        }

        // equals() method
        if(date1.equals(date2)) {
            System.out.println(date1 + " is equal to " + date2);
        }

        // compareTo() method
        int diff = date1.compareTo(date2);
        if(diff > 0) {
            System.out.println(date1 + " is greater than " + date2);
        } else if (diff < 0) {
            System.out.println(date1 + " is less than " + date2);
        } else {
            System.out.println(date1 + " is equal to " + date2);
        }
    }
}

使用 Calendar 类比较日期

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CompareCalendarExample {
    public static void main(String[] args) throws ParseException {

        Calendar cal1 = Calendar.getInstance();
        cal1.set(2020, 2, 20, 10, 30, 45);

        Calendar cal2 = Calendar.getInstance();
        cal2.set(2020, 12, 18, 14, 22, 30);


        // after() method
        if(cal1.after(cal2)) {
            System.out.println(cal1 + " is after " + cal2);
        }

        // before() method
        if(cal1.before(cal2)) {
            System.out.println(cal1 + " is before " + cal2);
        }

        // equals() method
        if(cal1.equals(cal2)) {
            System.out.println(cal1 + " is equal to " + cal2);
        }

        // compareTo() method
        int diff = cal1.compareTo(cal2);
        if(diff > 0) {
            System.out.println(cal1 + " is greater than " + cal2);
        } else if (diff < 0) {
            System.out.println(cal1 + " is less than " + cal2);
        } else {
            System.out.println(cal1 + " is equal to " + cal2);
        }
    }
}

相关文章