📜  Java中的 OffsetDateTime compareTo() 方法及示例

📅  最后修改于: 2022-05-13 01:55:02.492000             🧑  作者: Mango

Java中的 OffsetDateTime compareTo() 方法及示例

Java中 OffsetDateTime 类的compareTo()方法将此日期时间与另一个日期时间进行比较。
句法 :

public int compareTo(OffsetDateTime other)

参数:此方法接受一个参数other ,它指定要比较的另一个日期时间,而不是 null。
返回值:返回比较器的值,小于则为负,大于则为正。
下面的程序说明了compareTo()方法:
程序 1:

Java
// Java program to demonstrate the compareTo() method
 
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Parses the date1
        OffsetDateTime date1 = OffsetDateTime.parse("2018-12-12T13:30:30+05:00");
 
        // Parses the date2
        OffsetDateTime date2 = OffsetDateTime.parse("2018-12-12T13:30:30+05:00");
 
        // Prints both dates
        System.out.println("Date1: " + date1);
        System.out.println("Date2: " + date2);
 
        // Compare both
        System.out.println("On comparing we get " + date1.compareTo(date2));
    }
}


Java
// Java program to demonstrate the compareTo() method
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Parses the date1
        OffsetDateTime date1 = OffsetDateTime.parse("2018-12-12T13:30:30+05:00");
 
        // Parses the date2
        OffsetDateTime date2 = OffsetDateTime.parse("2015-12-12T13:30:30+05:00");
 
        // Prints both dates
        System.out.println("Date1: " + date1);
        System.out.println("Date2: " + date2);
 
        // Compare both
        System.out.println("On comparing we get " + date1.compareTo(date2));
    }
}


Java
// Java program to demonstrate the compareTo() method
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Parses the date1
        OffsetDateTime date1 = OffsetDateTime.parse("2013-12-12T13:30:30+05:00");
 
        // Parses the date2
        OffsetDateTime date2 = OffsetDateTime.parse("2015-12-12T13:30:30+05:00");
 
        // Prints both dates
        System.out.println("Date1: " + date1);
        System.out.println("Date2: " + date2);
 
        // Compare both
        System.out.println("On comparing we get " + date1.compareTo(date2));
    }
}


输出
Date1: 2018-12-12T13:30:30+05:00
Date2: 2018-12-12T13:30:30+05:00
On comparing we get 0

方案二

Java

// Java program to demonstrate the compareTo() method
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Parses the date1
        OffsetDateTime date1 = OffsetDateTime.parse("2018-12-12T13:30:30+05:00");
 
        // Parses the date2
        OffsetDateTime date2 = OffsetDateTime.parse("2015-12-12T13:30:30+05:00");
 
        // Prints both dates
        System.out.println("Date1: " + date1);
        System.out.println("Date2: " + date2);
 
        // Compare both
        System.out.println("On comparing we get " + date1.compareTo(date2));
    }
}
输出
Date1: 2018-12-12T13:30:30+05:00
Date2: 2015-12-12T13:30:30+05:00
On comparing we get 3

程序 3

Java

// Java program to demonstrate the compareTo() method
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Parses the date1
        OffsetDateTime date1 = OffsetDateTime.parse("2013-12-12T13:30:30+05:00");
 
        // Parses the date2
        OffsetDateTime date2 = OffsetDateTime.parse("2015-12-12T13:30:30+05:00");
 
        // Prints both dates
        System.out.println("Date1: " + date1);
        System.out.println("Date2: " + date2);
 
        // Compare both
        System.out.println("On comparing we get " + date1.compareTo(date2));
    }
}
输出
Date1: 2013-12-12T13:30:30+05:00
Date2: 2015-12-12T13:30:30+05:00
On comparing we get -2

参考:https: Java/time/temporal/TemporalAdjuster.html