java.time.LocalTime.compareTo() 方法示例



描述

java.time.LocalTime.compareTo(LocalTime other) 方法將此時間與其他時間進行比較。

宣告

以下是 java.time.LocalTime.compareTo(LocalTime other) 方法的宣告。

public int compareTo(LocalTime other)

引數

other − 用於進行比較的另一個時間,非空。

返回值

比較器值,小於則為負,大於則為正。

異常

NullPointerException − other 為空。

示例

以下示例展示了 java.time.LocalTime.compareTo(LocalTime other) 方法的用法。

package com.tutorialspoint;

import java.time.LocalTime;

public class LocalTimeDemo {
   public static void main(String[] args) {

      LocalTime time = LocalTime.parse("12:30:30");
      System.out.println(time);  
      LocalTime time1 = LocalTime.parse("12:35:30");
      System.out.println(time1);  
      System.out.println(time1.compareTo(time));  
   }
}

讓我們編譯並執行上述程式,將產生以下結果 −

12:30:30
12:35:30
1
廣告
© . All rights reserved.