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

持续时间类compareTo()方法 (Duration Class compareTo() method)

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

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

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

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

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

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

  • compareTo() method does not throw an exception at the time of comparing.

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

Syntax:

句法:

    public int compareTo(Duration d);

Parameter(s):

参数:

  • Duration d – represents the object to be compared to this Duration object.

    持续时间d –表示要与此持续时间对象进行比较的对象。

Return value:

返回值:

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

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

  • When (this Duration) == (Duration d), it returns 0.

    当(this Duration)==(Duration d)时,它返回0。

  • When (this Duration) < (Duration d), it returns -1.

    当(this Duration)<(Duration d)时,它返回-1。

  • When (this Duration) > (Duration d), it returns 1.

    当(this Duration)>(Duration d)时,返回1。

Example:

例:

// Java program to demonstrate the example 
// of int compareTo() method of Duration
import java.time.*;
import java.time.temporal.*;
public class CompareToOfDuration {
public static void main(String args[]) {
// Instantiates three Duration objects
Duration du1 = Duration.ofHours(2);
Duration du2 = Duration.ofMinutes(2);
Duration du3 = Duration.between(LocalTime.MIN, LocalTime.MAX);
System.out.println("du1: " + du1);
System.out.println("du2: " + du2);
System.out.println("du3: " + du3);
System.out.println();
// compares this Duration object(du1)
// to the given Duration object(du2) i.e here
// it returns 1 because the value of du1>du2
int compare = du1.compareTo(du2);
System.out.println("du1.compareTo(du2): " + compare);
// compares this Duration object(du2)
// to the given Duration object(du1) i.e here
// it returns -1 because the value of du2<du1
compare = du2.compareTo(du1);
System.out.println("du2.compareTo(du1): " + compare);
// compares this Duration object(du3)
// to the given Duration object(du3) i.e here
// it returns 0 because the value of du2==du1
compare = du3.compareTo(du3);
System.out.println("du3.compareTo(du3): " + compare);
}
}

Output

输出量

du1: PT2H
du2: PT2M
du3: PT23H59M59.999999999Sdu1.compareTo(du2): 1
du2.compareTo(du1): -1
du3.compareTo(du3): 0

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/541713.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

linux定时任务执行url,科技常识:linux定时任务访问url实例

今天小编跟大家讲解下有关linux定时任务访问url实例 &#xff0c;相信小伙伴们对这个话题应该也很关注吧&#xff0c;小编也收集到了有关linux定时任务访问url实例 的相关资料&#xff0c;希望小伙伴会喜欢也能够帮助大家。这次linux定时任务设置成功&#xff0c;也算是自己学习…

lcase和ucase_在SQL中使用UCASE(),LCASE()和MID()函数

lcase和ucaseUpper Case, Lower Case and MID functions are scalar functions which return a single value, based in the input value. 大写&#xff0c;小写和MID函数是标量函数&#xff0c;它们基于输入值返回单个值。 As you all know sometimes different databases ha…

Maven的Settings.xml配置文件解释

该配置用于单用户配置和全局配置, 单用户配置默认存放于 ${user.home}/.m2/目录中. 全局配置默认存放于Maven安装目录下面的conf目录中. 这两个默认的位置都可以修改. <?xml version"1.0" encoding"UTF-8"?> <settings xmlns"http://m…

linux ntp手动授时,关于我校NTP授时服务的使用说明

校园网用户&#xff1a;我中心于近期采购了GPS北斗授时服务设备&#xff0c;该设备可实现纯GPS模式、纯北斗模式和混合模式与卫星对时&#xff0c;同时实现对校内设备授时的功能。支持所有NTP协议的服务器、PC、嵌入式设备等&#xff0c;包括但不限于&#xff1a;Microsoft Win…

一串字符串转换为ascii_将ASCII字符串(char [])转换为C中的BYTE数组

一串字符串转换为asciiGiven an ASCII string (char[]) and we have to convert it into BYTE array (BYTE[]) in C. 给定一个ASCII字符串(char [])&#xff0c;我们必须将其转换为C语言中的BYTE数组(BYTE [])。 Logic: 逻辑&#xff1a; To convert an ASCII string to BYTE…

debugging Auto Layout:Logical Errors

Logical Errors逻辑错误 Logical errors are simply bugs. Somewhere, you have an assumption that is faulty. Perhaps it’s an assumption about how Auto Layout calculates the views’ frames. Perhaps it’s an assumption about the set of constraints that you’ve …

linux反序列化漏洞,思科多个产品Java反序列化漏洞(CVE-2015-6420)

思科多个产品Java反序列化漏洞(CVE-2015-6420)发布日期&#xff1a;2015-12-15更新日期&#xff1a;2015-12-17受影响系统&#xff1a;Cisco Unified ComputingCisco Voice and Unified Communications DevicesCisco Wireless描述&#xff1a;CVE(CAN) ID: CVE-2015-6420思科是…

密码学替代技术_替代技术及其类型| 密码学

密码学替代技术As we already discussed what are the Substitution techniques and one of its type Ceasar Cipher? So we are not discussing it here for that please refer to Cryptography: CeasarCipher here: Cryptography: Caesar Cipher and its Python Implementat…

Flask+uwsgi+Nginx环境搭建

2019独角兽企业重金招聘Python工程师标准>>> 开源软件准备 需要的软件列表&#xff1a; setuptools-33.1.1.zip Python-2.7.13.tgz pip-9.0.1.tar.gz nginx-1.10.3.tar.gz 软件统一上传到/usr/local/src/下&#xff0c;python是使用自己编译的。Python安装 先安装以…

穿越迷宫c语言程序设计教程课后答案,实验二 迷宫实验.doc

#include #define ROW 11#define COLUMN 15typedef struct{ /*栈中的数据元素的类型定义*/int row; /*行下标*/int col; /*列下标*/int direction; /*下一步移动方向*/} DATA;Typedif struct node{ /* 栈类定义*/DATA data;Struct node *next;}LinkStack;Typedef struct{/*移动…

ofb模式_密码学中的输出反馈模式(OFB)

ofb模式This is an output feedback (OFB) mode is similar in structure to that of CFB in Cryptography. It is the output of the encryption function that is fed back to the shift register in OFB in the cryptography, whereas in CFB in the mode of blocks, the ci…

JavaScript0-闭包

1.闭包的概念&#xff1a;在JavaScript中局部作用域总是能够访问到全局作用域&#xff0c;即内部函数总是能够访问到外部函数的参数和变量&#xff0c;即使内部函数调用完毕。也就是指有权访问到函数作用域里的变量。 function fn1() {var x 0;return function() {cosole.log(…

win8编程c语言,Win8系统怎么运行C语言 win8系统运行C语言的方法

C语言是一门通用计算机编程语言&#xff0c;是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言&#xff0c;但是许多win8系统用户并不知道要怎么运行C语言&#xff0c;针对这个情况&#xff0c;小编就给大家分享一…

stack示例_C.示例中的Stack.CopyTo()方法

stack示例C&#xff03;Stack.CopyTo()方法 (C# Stack.CopyTo() method) Stack.CopyTo() method is used to copy the stack elements/objects to an existing array from the given index. Stack.CopyTo()方法用于将堆栈元素/对象从给定索引复制到现有数组。 Syntax: 句法&am…

Linux sudoers文件的写法

2019独角兽企业重金招聘Python工程师标准>>> 文件的组成 sudoers文件由三部分组成&#xff1a; sudoers的默认配置&#xff0c;主要设置sudo的一些缺省值&#xff08;本文不会对这些默认配置进行介绍&#xff0c;若有兴趣可以自己man 5 sudoers然后搜defaults)alias…

if是什么c语言,这个C语言是什么(if(1))?

我在openssl源代码中注意到一个奇怪的成语,here并重复如下&#xff1a;if ((in NULL) && (passwds NULL)) {if (1) { (* #ifndef OPENSSL_NO_UI/* build a null-terminated list */static char *passwds_static[2] { NULL, NULL };passwds passwds_static;if (in …

c#queue_带有C#示例的Queue.CopyTo()方法

c#queueC&#xff03;Queue.CopyTo()方法 (C# Queue.CopyTo() method) Queue.CopyTo() method is used to copy the Queue elements/objects to an existing array from specified index. Queue.CopyTo()方法用于将Queue元素/对象从指定的索引复制到现有数组。 Syntax: 句法&a…

指针在c语言中的运用,怎么理解C语言中的指针,如何运用?

恰好我之前写了一系列介绍 C 语言的文章&#xff0c;介绍了什么是指针&#xff0c;以及为什么要使用指针&#xff0c;下面摘录一部分&#xff0c;感兴趣的话&#xff0c;可以点我了解更多。什么是 C语言指针&#xff1f;不同的数据类型的主要区别在于占用的存储空间不同。我们知…

设计模式(一)单例模式的七种写法

1. 饿汉模式 public class Singleton { private static Singleton instance new Singleton(); private Singleton (){}public static Singleton getInstance() { return instance; } } View Code这种方式在类加载时就完成了初始化&#xff0c;所以类加载较慢&#xff0c;…

scala 字符串转换数组_如何在Scala中将字节数组转换为字符串?

scala 字符串转换数组Byte Array in Scala is an array of elements of a byte type. String in Scala is a collection of the character data type. Scala中的字节数组是字节类型的元素的数组。 Scala中的String是字符数据类型的集合。 将字节数组转换为字符串 (Convert byt…