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

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

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

LocalTime 类compareTo()方法用于将此 LocalTime 对象与作为参数传递的 LocalTime 进行比较,以检查两个 LocalTime 是否相等。两个 LocalTimes 之间的比较是基于一天内当地时间的时间线位置。该方法返回的值确定如下:

  • 如果此 LocalTime 大于作为参数传递的 LocalTime,则返回正值
  • 如果此 LocalTime 等于作为参数传递的 LocalTime,则返回零 (0)
  • 如果此 LocalTime 小于作为参数传递的 LocalTime,则返回负值。

句法:

public int compareTo(LocalTime other)

参数:该方法接受一个参数LocalTime ,这是要比较的另一个LocalTime,它不应该为null。

返回值:该方法返回int值,返回值确定如下:

  • 如果此 LocalTime 大于作为参数传递的 LocalTime,则返回正值
  • 如果此 LocalTime 等于作为参数传递的 LocalTime,则返回零 (0)
  • 如果此 LocalTime 小于作为参数传递的 LocalTime,则返回负值。

下面的程序说明了 compareTo() 方法:

程序 1:当这个 LocalTime 更大时

// Java program to demonstrate
// LocalTime.compareTo() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalTime Objects
        LocalTime time1
            = LocalTime.parse("17:52:49");
        LocalTime time2
            = LocalTime.parse("13:08:00");
  
        // apply compareTo()
        int value = time1.compareTo(time2);
  
        // print LocalDateTime
        System.out.println("Int Value:" + value);
  
        if (value > 0)
            System.out.println("LocalTime1 is greater");
        else if (value == 0)
            System.out.println("LocalTime1 is equal to"
                               + " LocalTime2");
        else
            System.out.println("LocalTime2 is greater");
    }
}
输出:
Int Value:1
LocalTime1 is greater

程序 2:当通过 LocalTime 时更大

// Java program to demonstrate
// LocalTime.compareTo() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalTime Objects
        LocalTime time1
            = LocalTime.parse("07:12:29");
        LocalTime time2
            = LocalTime.parse("13:08:00");
  
        // apply compareTo()
        int value = time1.compareTo(time2);
  
        // print LocalDateTime
        System.out.println("Int Value:" + value);
  
        if (value > 0)
            System.out.println("LocalTime1 is greater");
        else if (value == 0)
            System.out.println("LocalTime1 is equal to"
                               + " LocalTime2");
        else
            System.out.println("LocalTime2 is greater");
    }
}
输出:
Int Value:-1
LocalTime2 is greater

方案 3:当两个 Localtime 相等时

// Java program to demonstrate
// LocalTime.compareTo() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalTime Objects
        LocalTime time1
            = LocalTime.parse("13:08:00");
        LocalTime time2
            = LocalTime.parse("13:08:00");
  
        // apply compareTo()
        int value = time1.compareTo(time2);
  
        // print LocalDateTime
        System.out.println("Int Value:" + value);
  
        if (value > 0)
            System.out.println("LocalTime1 is greater");
        else if (value == 0)
            System.out.println("LocalTime1 is equal to"
                               + " LocalTime2");
        else
            System.out.println("LocalTime2 is greater");
    }
}
输出:
Int Value:0
LocalTime1 is equal to LocalTime2

参考: https: Java Java.time.LocalTime))