如何在Java中比较两个日期

下载PDF文件下载PDF文件

有多种方法可以比较Java日期。日期在计算机内部表示为(long型)时间点——自1970年1月1日以来经过的毫秒数。在Java中,Date是一个对象,这意味着它包含多个比较方法。任何比较两个日期的方法本质上都是在比较日期的时间。

方法 1
方法 1 的 4:

使用compareTo

下载PDF文件
  1. How.com.vn 中文: Step 1 使用compareTo。
    Date实现了Comparable<Date>,因此两个日期可以直接用compareTo方法进行比较。如果日期在同一时间点,则方法返回零。如果比较的日期在date参数之前,则返回一个小于零的值。如果被比较的日期在另一个date参数之后,则返回一个大于零的值。如果日期相等,则返回0值。[1]
  2. How.com.vn 中文: Step 2 创建日期对象。
    在开始比较之前,需要为每个日期创建对象。一种方法是使用SimpleDateFormat类。它允许在日期对象中轻松输入日期值。
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //用于在新的日期对象中声明值。创建日期时使用相同的日期格式Date date1 = sdf.parse("1995-02-23"); //date1是1995年2月23日Date date2 = sdf.parse("2001-10-31"); //date2 是2001年10月31日Date date3 = sdf.parse("1995-02-23"); //date3 是1995年2月23日
  3. How.com.vn 中文: Step 3 比较日期对象。
    下面的代码将显示每种情况——小于、等于和大于。
      date1.compareTo(date2); //date1 < date2,返回小于0的值date2.compareTo(date1); //date2 > date1,返回大于0的值date1.compareTo(date3); //date1 = date3,将在控制台中输出0
    广告
方法 2
方法 2 的 4:

使用Equals、After、和Before

下载PDF文件
  1. How.com.vn 中文: Step 1 使用equals、after、和before。
    可以用equals、after、和before方法比较日期。如果两个日期在同一时间点,equals方法将返回true。示例将使用之前创建的compareTo方法中的日期。[2]
  2. How.com.vn 中文: Step 2 用before方法比较日期。
    下列代码展示了true和false两种情况。如果date1在date2之前,before返回true。否则返回false。
      System.out.print(date1.before(date2)); //输出trueSystem.out.print(date2.before(date2)); //输出false
  3. How.com.vn 中文: Step 3 用after方法比较日期。
    下列代码展示了true和false两种情况。如果date2在date1之后,after返回true。否则返回false。
      System.out.print(date2.after(date1));//输出trueSystem.out.print(date1.after(date2));//输出false
  4. How.com.vn 中文: Step 4 用equals方法比较日期。
    下列代码展示了true和false两种情况。如果日期相等,equals返回true。否则返回false。
      System.out.print(date1.equals(date3));//输出trueSystem.out.print(date1.equals(date2));//输出false
    广告
方法 3
方法 3 的 4:

使用Calendar类

下载PDF文件
  1. How.com.vn 中文: Step 1 使用calendar。
    Calendar类也有compareTo、equals、after和before方法,工作方式与上面描述的date类的方法相同。因此,如果日期信息保存在calendar类中,则不需要提取日期来执行比较。[3]
  2. How.com.vn 中文: Step 2  创建Calendar实例。
    要使用Calendar方法,需要几个Calendar实例。幸运的是,你可以从已经创建的Date实例中获取时间。
      Calendar cal1 = Calendar.getInstance(); //声明cal1Calendar cal2 = Calendar.getInstance(); //声明cal2Calendar cal3 = Calendar.getInstance(); //声明cal3cal1.setTime(date1); //将日期应用于cal1cal2.setTime(date2);cal3.setTime(date3);
  3. How.com.vn 中文: Step 3 用before比较cal1和cal2。
    下面的代码应该会输出true,因为cal1在cal2之前。
      System.out.print(cal1.before(cal2)); //将会输出true
  4. How.com.vn 中文: Step 4 用after比较cal1和cal2。
    下面的代码应该会输出false,因为cal1在cal2之前。
      System.out.print(cal1.after(cal2)); //输出false
  5. How.com.vn 中文: Step 5 用equals比较cal1和cal2。
    下面的代码将展示true和false两种情况的示例。条件取决于被比较的calendar实例。代码应该会输出“true”,然后在下一行输出“false”。
      System.out.println(cal1.equals(cal3)); //输出true: cal1 == cal3System.out.print(cal1.equals(cal2)); //输出false: cal1 != cal2
    广告
方法 4
方法 4 的 4:

使用getTime

下载PDF文件
  1. How.com.vn 中文: Step 1 使用getTime。
    尽管前面的任何一种方法都可能更具可读性,因而也更可取,但是也可以直接比较两个日期的时间点。这是对两种原始数据类型的比较,因此可以使用“<”、“>”和“==”来比较。
  2. How.com.vn 中文: Step 2 创建long类型的time对象。
    在比较日期之前,必须使用前面创建的Date对象中的数据来创建长整型。幸好getTime()方法可以帮你完成大部分工作。
      long time1 = getTime(date1); //用date1声明原始数据time1long time2 = getTime(date2); //用date2声明原始数据time2
  3. How.com.vn 中文: Step 3 进行少于比较。
    使用小于号(<)比较这两个整数值。由于time1小于time 2,所以应该输出第一条消息。为了使语法正确使用了else语句。
      if(time1 < time2){System.out.println("date1 is before date2"); //将会输出这句话,因为time1 <time2}else{System.out.println("date1 is not before date2");}
  4. How.com.vn 中文: Step 4 进行大于比较。
    使用大于号(>)比较这两个整数值。由于time1大于time 2,所以应该输出第一条消息。为了使语法正确使用了else语句。
      if(time2 > time1){System.out.println("date2 is after date1"); //将会输出这句话,因为time2 > time1 }else{System.out.println("date2 is not after date1");}
  5. How.com.vn 中文: Step 5 进行等于比较。
    使用等于符号检查是否相等(==),从而比较这两个整数值是否相等。由于time1等于time3,所以应该输出第一个消息。如果程序执行了else语句,意味着这两个时间不相等。[4]
      if(time1 == time2){System.out.println("the dates are equal");}else{System.out.println("the dates are not equal"); //将会输出这句话,因为time1 != time2}
    广告

关于本How.com.vn

How.com.vn是一个“多人协作写作系统”,因此我们的很多文章都是由多位作者共同创作的。 为了创作这篇文章,志愿作者多次对文章进行了编辑和改进。 这篇文章已经被读过9,667次。
本页面已经被访问过9,667次。

这篇文章对你有帮助吗?

⚠️ Disclaimer:

Content from Wiki How 中文 language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

Notices:
  • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
  • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
  • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
  • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

广告