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.pswp.cn/news/541713.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541713.shtml
英文地址,請注明出處:http://en.pswp.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…