集册 Java实例教程 比较两个日期

比较两个日期

欢马劈雪     最近更新时间:2020-01-02 10:19:05

408
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用compareTo()方法之一,该方法是日期时间API类的一部分。
/** from 
时 代 J a v a 公 众 号**/

import java.time.LocalDate;

import java.time.Month;


public class Main {


  public static void main(String[] args) {

    LocalDate ldt1 = LocalDate.of(2000, Month.NOVEMBER, 11);

    LocalDate ldt2 = LocalDate.now();

    int comparison = ldt1.compareTo(ldt2);

    if (comparison > 0) {

      System.out.println(ldt1 + " is after " + ldt2);

    } else if (comparison < 0) {

      System.out.println(ldt1 + " is before " + ldt2);

    } else {

      System.out.println(ldt1 + " is equal to " + ldt2);

    }/* from N o w J a v a . c o m - 时  代  Java*/


    if (ldt1.isAfter(ldt2)) {

      System.out.println(ldt1 + " is after " + ldt2);

    } else if (ldt1.isBefore(ldt2)) {

      System.out.println(ldt1 + 
展开阅读全文