即时类compareTo()方法 (Instant Class compareTo() method)

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

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

  • compareTo() method is used to compare this Instant object to the given object.

    compareTo()方法用于将此Instant对象与给定对象进行比较。

  • compareTo() method is a non-static method, it is accessible with the class object only 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.

    compareTo()方法在比较时可能会引发异常。

    NullPointerException: This exception may throw when the given parameter is null exists.

    NullPointerException :当给定参数为null时,可能引发此异常。

Syntax:

句法:

    public int compareTo(Instant ins);

Parameter(s):

参数:

  • Instant ins – represents the object to be compared to this Instant.

    Instant ins –表示要与此Instant进行比较的对象。

Return value:

返回值:

The return type of this method is int, it may return anyone of the given values,

此方法的返回类型为int ,它可以返回任何给定值,

  • If (this Instant) < (Instant ins), it returns -1.

    如果(此Instant)<(Instant ins),则返回-1。

  • If (this Instant) > (Instant ins), it returns 1.

    如果(此Instant)>(Instant ins),则返回1。

  • If (this Instant) == (Instant ins), it returns 0.

    如果(this Instant)==(Instant ins),则返回0。

Example:

例:

// Java program to demonstrate the example
// of int compareTo() method of Instant
import java.time.*;
public class CompareOfInstant {public static void main(String args[]) {// Instantiates two Instant
Instant ins1 = Instant.parse("2006-04-03T05:10:15.00Z");
Instant ins2 = Instant.parse("2007-06-05T10:20:30.00Z");
// Display ins1,ins2
System.out.println("Instant ins1 and ins2: ");
System.out.println("ins1: " + ins1);
System.out.println("ins2: " + ins2);
System.out.println();
// Here, this method compares this Instant
// (ins1) to the given Instant(ins2) i.e.
// here it returns -1 because ins1 < ins2
int compare = ins1.compareTo(ins2);
// Display compare
System.out.println("ins1.compareTo(ins2): " + compare);
// Here, this method compares this Instant
// (ins1) to the given Instant(ins1) i.e.
// here it returns 0 because ins1 == ins1
compare = ins1.compareTo(ins1);
// Display compare
System.out.println("ins1.compareTo(ins1): " + compare);
// Here, this method compares this Instant
// (ins2) to the given Instant(ins1) i.e.
// here it returns 1 because ins1 > ins2
compare = ins2.compareTo(ins1);
// Display compare
System.out.println("ins2.compareTo(ins1): " + compare);
}
}

Output

输出量

Instant ins1 and ins2:
ins1: 2006-04-03T05:10:15Z
ins2: 2007-06-05T10:20:30Zins1.compareTo(ins2): -1
ins1.compareTo(ins1): 0
ins2.compareTo(ins1): 1

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

Java即时类| 带示例的compareTo()方法相关推荐

  1. Java Duration类| 带示例的compareTo()方法

    持续时间类compareTo()方法 (Duration Class compareTo() method) compareTo() method is available in java.time ...

  2. Java LocalDate类| 带示例的compareTo()方法

    LocalDate类compareTo()方法 (LocalDate Class compareTo() method) compareTo() method is available in java ...

  3. Java LocalDateTime类| 带示例的compareTo()方法

    LocalDateTime类compareTo()方法 (LocalDateTime Class compareTo() method) compareTo() method is available ...

  4. java中get接口示例_Java即时类| 带示例的get()方法

    java中get接口示例 即时类的get()方法 (Instant Class get() method) get() method is available in java.time package ...

  5. getlong_Java即时类| 带示例的getLong()方法

    getlong 即时类getLong()方法 (Instant Class getLong() method) getLong() method is available in java.time p ...

  6. Java BigInteger类| 带示例的shiftLeft()方法

    BigInteger类shiftLeft()方法 (BigInteger Class shiftLeft() method) shiftLeft() method is available in ja ...

  7. Java LocalDate类| 带示例的getDayOfYear()方法

    LocalDate类的getDayOfYear()方法 (LocalDate Class getDayOfYear() method) getDayOfYear() method is availab ...

  8. Java LocalDateTime类| 带示例的getDayOfWeek()方法

    LocalDateTime类getDayOfWeek()方法 (LocalDateTime Class getDayOfWeek() method) getDayOfWeek() method is ...

  9. Java LocalDate类| 带示例的format()方法

    LocalDate类format()方法 (LocalDate Class format() method) format() method is available in java.time pac ...

最新文章

  1. luogu P2865 [USACO06NOV]Roadblocks G(次短路模板)
  2. Nature:深大李猛组揭示阿斯加德古菌新门(悟空古菌)及其与真核生物的关系
  3. 解决Redis超卖问题
  4. python 排列组合_python解决排列组合
  5. Some Experience about userControls
  6. ASP.NET Core Web Api之JWT(一)
  7. html js倒计时不准确,js倒计时代码:第2个倒计时为什么会不正常?
  8. 知名互联网公司系统架构图[第2期]
  9. 使用 shell 在多服务器上批量操作
  10. 极光im php,利用php+curl调用极光IM第三方REST API方法经验
  11. Saved Blogs
  12. 小姜技术博客开通啦!
  13. 让游戏在英国取得成功的 5 个技巧
  14. VisualStudio/VS在一个项目中添加多个c++文件
  15. python快速开发app_python 使用Airtest超快速开发App爬虫
  16. 逻辑越权——垂直、水平越权
  17. 恶搞!关闭程序进程!!嘿嘿嘿~
  18. Cauchy–Schwarz inequality理解
  19. HTML精仿ios相册,高仿ios相册地图功能
  20. ncvv -V 不是内部或外部命令,也不是可运行的程序

热门文章

  1. 倩女幽魂服务器维护时间,9月5日在线维护公告
  2. innobackupex远程备份脚本
  3. Xtrabackup备份与恢复
  4. 配置文件bashrc与profile的区别
  5. Angular Material 阴影使用
  6. C#判断回文字符串【C#】
  7. 【数据结构和算法05】 红-黑树(转发)
  8. Python基础语法学习整理
  9. ZZULI 1876: 蛤玮的项链 Hash + 二分
  10. Apache多站点配置详解