Java LocalDate类| 带示例的compareTo()方法
LocalDate類compareTo()方法 (LocalDate Class compareTo() method)
compareTo() method is available in java.time package.
compareTo()方法在java.time包中可用。
compareTo() method is used to compare this LocalDate object to the given object.
compareTo()方法用于將此LocalDate對(duì)象與給定對(duì)象進(jìn)行比較。
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()方法是一種非靜態(tài)方法,只能通過(guò)類對(duì)象訪問(wèn),如果嘗試使用類名稱訪問(wèn)該方法,則會(huì)收到錯(cuò)誤消息。
compareTo() method does not throw an exception at the time of comparing.
compareTo()方法在比較時(shí)不會(huì)引發(fā)異常。
Syntax:
句法:
public int compareTo(ChronoLocalDate l_date);Parameter(s):
參數(shù):
ChronoLocalDate l_date – represents the object to be compared to this LocalDate.
ChronoLocalDate l_date –表示要與此LocalDate比較的對(duì)象。
Return value:
返回值:
The return type of this method is int, it may return anyone of the given values,
此方法的返回類型為int ,它可以返回任何給定值,
When (this LocalDate) < (ChronoLocalDate l_date), it returns -ve.
當(dāng)(this LocalDate)<(ChronoLocalDate l_date)時(shí),它返回-ve。
When (this LocalDate) > (ChronoLocalDate l_date), it returns +ve.
當(dāng)(this LocalDate)>(ChronoLocalDate l_date)時(shí),它返回+ ve。
When (this LocalDate) == (ChronoLocalDate l_date), it returns 0.
當(dāng)(this LocalDate)==(ChronoLocalDate l_date)時(shí),它返回0。
Example:
例:
// Java program to demonstrate the example // of int compareTo(ChronoLocalDate l_date) // method of LocalDateimport java.time.*;public class CompareToOfLocalDate {public static void main(String args[]) {// Instantiates two LocalDateLocalDate l_da1 = LocalDate.parse("2007-04-04");LocalDate l_da2 = LocalDate.now();// Display l_da1,l_da2System.out.println("LocalDate l_da1 and l_da2: ");System.out.println("l_da1: " + l_da1);System.out.println("l_da2: " + l_da2);System.out.println();// Here, this method compares this date object to// the given date object i.e. it returns -ve// value because l_da1 = 2007-04-04 and l_da2// = 2020-05-08 so l_da1 - l_da2 = 2007-2020// i.e. -13 will be returnedint compare = l_da1.compareTo(l_da2);// Display compareSystem.out.println("l_da1.compareTo(l_da2): " + compare);// Here, this method compares this date object to// the given date object i.e. it returns +ve// value because l_da1 = 2007-04-04 and l_da2// = 2020-05-08 so l_da2 - l_da1 = 2020-2007// i.e. 13 will be returnedcompare = l_da2.compareTo(l_da1);// Display compareSystem.out.println("l_da2.compareTo(l_da1): " + compare);// Here, this method compares this date object to// the given date object i.e. it returns 0// because l_da1 == l_da1compare = l_da1.compareTo(l_da1);// Display compareSystem.out.println("l_da1.compareTo(l_da1): " + compare);} }Output
輸出量
LocalDate l_da1 and l_da2: l_da1: 2007-04-04 l_da2: 2020-05-29l_da1.compareTo(l_da2): -13 l_da2.compareTo(l_da1): 13 l_da1.compareTo(l_da1): 0翻譯自: https://www.includehelp.com/java/localdate-compareto-method-with-example.aspx
總結(jié)
以上是生活随笔為你收集整理的Java LocalDate类| 带示例的compareTo()方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: hypot函数_hypot()函数与C
- 下一篇: kotlin键值对数组_Kotlin程序