当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java ZoneOffsetTransition compareTo()用法及代码示例


java.time.zone.ZoneOffsetTransition类的compareTo()方法用于在ZoneOffsetTransition类型的两个对象之间进行比较。根据过渡时刻进行比较。

用法:

public int compareTo(ZoneOffsetTransition transition)

参数:此方法采用ZoneoffsetTransition类型的过渡,并将其与此过渡进行比较。

返回值:如果ZoneOffsetTransition的两个对象相等,则此方法返回零;如果传递的过渡小于此过渡,则返回负值;如果传递的过渡大于此过渡,则返回正值。

下面是说明compareTo()方法的示例:



范例1:

// Java program to demonstrate 
// compareTo() method 
  
import java.util.*; 
import java.io.*; 
import java.time.*; 
import java.time.chrono.*; 
import java.time.zone.*; 
  
public class GFG { 
    public static void main(String[] argv) 
    { 
  
        // Creating and initializing the 
        // object of LocalDateTime 
        LocalDateTime loc 
            = LocalDateTime.of( 
                1999, 04, 25, 03, 
                24, 45, 0); 
  
        // Creating and initializing the 
        // object of ZoneOffset 
        ZoneOffset off1 
            = ZoneOffset.ofTotalSeconds( 
                8); 
  
        // Creating and initializing the 
        // object of ZoneOffset 
        ZoneOffset off2 
            = ZoneOffset.ofTotalSeconds( 
                12); 
  
        // Creating and initializing 
        // ZoneOffsetTransition Object 
        ZoneOffsetTransition zonetrans1 
            = ZoneOffsetTransition.of( 
                loc, off1, off2); 
  
        // Creating and initializing 
        // ZoneOffsetTransition Object 
        ZoneOffsetTransition zonetrans2 
            = ZoneOffsetTransition.of( 
                loc, off1, off2); 
  
        // Comparing both object using 
        // compareTo() method 
        int status 
            = zonetrans1.compareTo( 
                zonetrans2); 
  
        // Display the result 
        if (status == 0) 
            System.out.println( 
                "zonetrans1 is equal to zonetrans2"); 
        else if (status > 0) 
            System.out.println( 
                "zonetrans1 is greater than zonetrans2"); 
        else
            System.out.println( 
                "zonetrans1 is lesser than zonetrans2"); 
    } 
}
输出:
zonetrans1 is equal to zonetrans2

范例2:

// Java program to demonstrate 
// compareTo() method 
  
import java.util.*; 
import java.io.*; 
import java.time.*; 
import java.time.chrono.*; 
import java.time.zone.*; 
  
public class GFG { 
    public static void main(String[] argv) 
    { 
  
        // Creating and initializing the 
        // object of LocalDateTime 
        LocalDateTime loc 
            = LocalDateTime.of( 
                1999, 04, 25, 03, 
                24, 45, 0); 
  
        // Creating and initializing the 
        // object of ZoneOffset 
        ZoneOffset off1 
            = ZoneOffset.ofTotalSeconds( 
                8); 
  
        // Creating and initializing the 
        // object of ZoneOffset 
        ZoneOffset off2 
            = ZoneOffset.ofTotalSeconds( 
                12); 
  
        // Creating and initializing 
        // ZoneOffsetTransition Object 
        ZoneOffsetTransition zonetrans1 
            = ZoneOffsetTransition.of( 
                loc, off1, off2); 
  
        // Creating and initializing 
        // ZoneOffsetTransition Object 
        ZoneOffsetTransition zonetrans2 
            = ZoneOffsetTransition.of( 
                loc, off2, off1); 
  
        // Comparing both object using 
        // compareTo() method 
        int status 
            = zonetrans1.compareTo( 
                zonetrans2); 
  
        // Display the result 
        if (status == 0) 
            System.out.println( 
                "zonetrans1 is equal to zonetrans2"); 
        else if (status > 0) 
            System.out.println( 
                "zonetrans1 is greater than zonetrans2"); 
        else
            System.out.println( 
                "zonetrans1 is lesser than zonetrans2"); 
    } 
}
输出:
zonetrans1 is greater than zonetrans2

参考:https://docs.oracle.com/javase/9/docs/api/java/time/zone/ZoneOffsetTransition.html#compareTo-java.time.zone.ZoneOffsetTransition-




相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 ZoneOffsetTransition compareTo() method in Java with Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。