博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java日历compareTo()方法与示例
阅读量:2531 次
发布时间:2019-05-11

本文共 2730 字,大约阅读时间需要 9 分钟。

日历类的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 Calendarimport 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 2020ca2: Wed Jan 23 11:51:26 GMT 2030ca1.compareTo(ca2): -1

翻译自:

转载地址:http://ratzd.baihongyu.com/

你可能感兴趣的文章
HTML基础之JS
查看>>
01背包问题python 2.7实现
查看>>
E - Kagome Kagome
查看>>
Android Studio Errors
查看>>
软件工程第二次作业—结对编程
查看>>
【字符编码】Java字符编码详细解答及问题探讨
查看>>
学习操作系统导图
查看>>
在线的JSON formate工具
查看>>
winform非常实用的程序退出方法!!!!!(转自博客园)
查看>>
xml解析
查看>>
centos安装vim
查看>>
linux工作调度(计划任务)
查看>>
HTTP协议形象展现
查看>>
hdu--1698 Just a Hook(线段树+区间更新+懒惰标记)
查看>>
Django 框架的 模板继承 与 模板包含
查看>>
Effective Modern C++:01类型推导
查看>>
根据html页面模板动态生成html页面(c#类)
查看>>
Failed to Connect to MySQL at 127.0.0.1:3306 MySql WorkBench 本地连接问题
查看>>
android下activity中多个listview只允许主界面滚动
查看>>
(贪心5.2.1)UVA 10026 Shoemaker's Problem(利用数据有序化来进行贪心选择)
查看>>