日历类的compareTo()方法 (Calendar Class compareTo() method)

  • compareTo() method is available in java.util package.

    compareTo()方法在java.util包中可用。

  • compareTo() method is used to compare two Calendar objects or in other words, we can say this method is used to compare the time of this Calendar object and the given Calendar object.

    compareTo()方法用于比较两个Calendar对象,换句话说,可以说该方法用于比较此Calendar对象和给定Calendar对象的时间。

  • compareTo() method is a non-static method, it is accessible with the class object and if we try to access the method with the class name then we will get an error.

    compareTo()方法是一种非静态方法,可通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • compareTo() method may throw an exception at the time of comparing two Calendar objects.

    比较两个Calendar对象时, compareTo()方法可能会引发异常。

    • NullPointerException: This exception may throw when the given parameter is null exists.NullPointerException :当给定参数为null时,可能引发此异常。
    • IllegalArgumentException: This exception may throw when the time of the given Calendar object is illegal.IllegalArgumentException :如果给定Calendar对象的时间非法,则可能引发此异常。

Syntax:

句法:

    public int compareTo(Calendar obj2);

Parameter(s):

参数:

  • Calendar obj – represents the Calendar object to be compared with this Calendar object.

    Calendar obj –表示要与此日历对象进行比较的Calendar对象。

Return value:

返回值:

The return type of this method is int, it returns the following values based on the below given cases,

此方法的返回类型为int ,它基于以下给定情况返回以下值:

  • It returns 0 if this Calendar time value is the same as the given Calendar time.

    如果此日历时间值与给定的日历时间相同,则返回0

  • It returns the value < 0 if the time denoted by this Calendar is before the time denoted by the given Calendar parameter.

    如果此Calendar表示的时间早于给定Calendar参数表示的时间,它将返回值<0

  • It returns the value > 0 if this Calendar time is after the time denoted by the given Calendar parameter.

    如果此日历时间晚于给定Calendar参数指示的时间,它将返回值> 0

Example:

例:

// Java Program to demonstrate the example of
// int compareTo(Object) method of Calendar
import java.util.*;
public class CompareOfCalendar {public static void main(String[] args) {// Instantiating two Calendar object
Calendar ca1 = Calendar.getInstance();
Calendar ca2 = Calendar.getInstance();
// By using add() method to add the 10 years
// in ca2 to the current ca1
ca2.add(Calendar.YEAR, 10);
// Display ca1 and ca2
System.out.println("ca1: " + ca1.getTime());
System.out.println("ca2: " + ca2.getTime());
// By using compareTo(Object) method is to
// compare two calendar ca1 and ca2
int comp = ca1.compareTo(ca2);
// Display compared result
System.out.println("ca1.compareTo(ca2): " + comp);
}
}

Output

输出量

ca1: Thu Jan 23 11:51:26 GMT 2020
ca2: Wed Jan 23 11:51:26 GMT 2030
ca1.compareTo(ca2): -1

翻译自: https://www.includehelp.com/java/calendar-compareto-method-with-example.aspx

Java日历compareTo()方法与示例相关推荐

  1. Java Date compareTo()方法具有什么功能呢?

    转自: Java Date compareTo()方法具有什么功能呢? Date compareTo()方法的功能简介说明:对比两个Date值的大小Date compareTo()方法的语法:publ ...

  2. Java IOUtils.copy方法代码示例(亲测)

    本文整理汇总了Java中org.apache.commons.io.IOUtils.copy方法的典型用法代码示例.如果您正苦于以下问题:Java IOUtils.copy方法的具体用法?Java I ...

  3. Java中CompareTo()方法的详细介绍

    Java中CompareTo()方法: String 字符串用compareTo方法从第一位开始比较, 如果遇到不同的字符,则马上返回这两个字符的ascii值差值.返回值是int类型 1.当两个比较的 ...

  4. java compareto long_Java Long类compareTo()方法与示例

    Long类compareTo()方法compareTo()方法在java.lang包中可用. compareTo()方法用于在数学上检查该Long对象与给定Long对象的相等性或不相等性,换句话说,可 ...

  5. JAVA中返回值为字母时_LeetCode#524通过删除字母匹配到字典里最长单词-java中CompareTo方法用法以及Comparator中Compare方法返回值...

    import java.util.Collections; import java.util.Comparator; import java.util.List; /* 524. 通过删除字母匹配到字 ...

  6. java重写compareTo()方法,比较对象的大小

    理论: 使用Comparable或Comparator两个接口中的任何一个来比较对象的大小  一.Comparable接口的使用,自然排序       1.像String.BigDecimal.包装类 ...

  7. java user directory,Java ProcessBuilder directory()方法与示例

    语法:public File directory (); public ProcessBuilder directory (File dir); ProcessBuilder类directory()方 ...

  8. Java序列化魔术方法及其示例使用

    在上一篇文章中, 您需要了解有关Java序列化的所有知识 ,我们讨论了如何通过实现Java序列化来启用类的可序列化性. Serializable接口. 如果我们的类未实现Serializable接口, ...

  9. catalog java,Java Connection getCatalog()方法与示例

    通常,目录是一个目录,其中包含有关数据集,文件或数据库的信息.而数据库目录中包含所有数据库,基本表,视图(虚拟表),同义词,值范围,索引,用户和用户组的列表. Connection接口的getCata ...

最新文章

  1. 产品设计中的点线面法则
  2. Linux Shell基础 - 流程控制 - for循环 - while 循环 - until循环
  3. 网卡驱动:stmmac DMA发送流程
  4. MS SQL 不常用但却十分有用的一些语法
  5. java运动员最佳配对_运动员最佳配对问题 - osc_y1pyjby5的个人空间 - OSCHINA - 中文开源技术交流社区...
  6. 澳门大学物联网设计方法研究获“973”立项
  7. MySQL:给表建立索引及索引的显示
  8. laravel mysql 视图_Laravel框架控制器,视图及模型操作图文详解
  9. 计算机基础e卷,大学计算机基础(e卷).doc
  10. 中值滤波器 ( Median Filter ) C++ 实现
  11. 陆奇:欢迎毛圣博加入奇绩,成为奇绩创坛合伙人
  12. python求曲线拐点_如何发现拐点?
  13. node-sass安装失败完美解决方法
  14. 笔记暂记15:陪集,商集
  15. bootstrap表格自动换行
  16. JavaScript(1)使用ducument.write()在页面上显示红色的“开启JavaScript学习之旅”。
  17. 什么是锁?有几种锁?怎么用锁?
  18. XML文件约束之DTD详解
  19. 给大家推荐一下常用的镜像站
  20. 网易云音乐用户信息爬取以及可视化

热门文章

  1. 十个模块_专栏 | ABAQUS Part模块的十个小技巧
  2. cad常青藤插件_原来还有这么好用的CAD插件,半小时就能做完一张图
  3. locust mysql_locust性能压测连接mysql,随机取出班级,绑定学生
  4. ANSIBLE--handlers的概念
  5. 递归基础之N皇后问题
  6. mysql备份:一,Xtrabackup
  7. HDU 5777 domino
  8. Javascript 事件冒泡处理
  9. 颠覆与重构——戴尔助力徐工集团等行业客户实现业务转型
  10. Redhat linux 5.3 基于bond的heartbeat