Java方法中ChronoZonedDateTime接口的compareTo()方法將此日期與另一個日期進行比較。
用法:
default int compareTo(ChronoZonedDateTime other)
參數:此方法接受參數other,該參數指定要與之比較的其他日期,並且不專門為null。
返回值:返回比較器值,如果比較器值較小,則為負;如果比較大,則為正。
以下程序說明了Java中ChronoZonedDateTime的compareTo()方法:
程序1:
// Program to illustrate the compareTo() method
import java.util.*;
import java.time.*;
import java.time.chrono.*;
public class GfG {
public static void main(String[] args)
{
// First date
ChronoZonedDateTime dt
= ZonedDateTime.parse(
"2018-12-06T19:21:12.123+05:30[Asia/Calcutta]");
System.out.println(dt);
// Second date
ChronoZonedDateTime dt1
= ZonedDateTime.parse(
"2018-12-06T19:21:12.123+05:30[Asia/Calcutta]");
System.out.println(dt1);
try {
// Compare both dates
System.out.println(dt1.compareTo(dt));
}
catch (Exception e) {
System.out.println(e);
}
}
}
輸出:
2018-12-06T19:21:12.123+05:30[Asia/Calcutta] 2018-12-06T19:21:12.123+05:30[Asia/Calcutta] 0
程序2:
// Program to illustrate the compareTo() method
import java.util.*;
import java.time.*;
import java.time.chrono.*;
public class GfG {
public static void main(String[] args)
{
// First date
ChronoZonedDateTime dt
= ZonedDateTime.parse(
"2018-10-25T23:12:31.123+02:00[Europe/Paris]");
System.out.println(dt);
// Second date
ChronoZonedDateTime dt1
= ZonedDateTime.parse(
"2018-12-06T19:21:12.123+05:30[Asia/Calcutta]");
System.out.println(dt1);
try {
// Compare both dates
System.out.println(dt1.compareTo(dt));
}
catch (Exception e) {
System.out.println(e);
}
}
}
輸出:
2018-10-25T23:12:31.123+02:00[Europe/Paris] 2018-12-06T19:21:12.123+05:30[Asia/Calcutta] 1
相關用法
- Java ChronoZonedDateTime until()用法及代碼示例
- Java ChronoZonedDateTime get()用法及代碼示例
- Java ChronoZonedDateTime from()用法及代碼示例
- Java ChronoZonedDateTime with(TemporalAdjuster)用法及代碼示例
- Java ChronoZonedDateTime withZoneSameLocal()用法及代碼示例
- Java ChronoZonedDateTime toLocalTime()用法及代碼示例
- Java ChronoZonedDateTime hashCode()用法及代碼示例
- Java ChronoZonedDateTime getZone()用法及代碼示例
- Java ChronoZonedDateTime toInstant()用法及代碼示例
- Java ChronoZonedDateTime withEarlierOffsetAtOverlap()用法及代碼示例
- Java ChronoZonedDateTime toString()用法及代碼示例
- Java ChronoZonedDateTime withZoneSameInstant()用法及代碼示例
- Java ChronoZonedDateTime toLocalDateTime()用法及代碼示例
- Java ChronoZonedDateTime getChronology()用法及代碼示例
- Java ChronoZonedDateTime withLaterOffsetAtOverlap()用法及代碼示例
注:本文由純淨天空篩選整理自ShubhamMaurya3大神的英文原創作品 ChronoZonedDateTime compareTo() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。