集册 Java实例教程 使用compareTo方法比较两个日期对象

使用compareTo方法比较两个日期对象

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

393
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用compareTo方法比较两个日期对象
/** 
 来自 n o w j a v a . c o m**/

 

import java.util.Date;

 

public class Main {

 

  public static void main(String[] args) {

    Date d1 = new Date();

    try{

      Thread.sleep(10);

    }catch(Exception e){

    }

    Date d2 = new Date();

   

    System.out.println("First Date : " + d1);

    System.out.println("Second Date : " + d2);

   

    int results = d1.compareTo(d2);

   

    if(results > 0)

      System.out.println("First Date is after second");

    else if (results < 0)// 来自 n o w j a v a . c o m

      System.
展开阅读全文