📌  相关文章
📜  Java中的 OffsetTime isBefore() 方法及示例

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

Java中的 OffsetTime isBefore() 方法及示例

Java格式的 OffsetTime 类的isBefore()方法检查传递的时间是否早于参数中指定的时间。

句法:

public boolean isBefore(OffsetTime other)

参数:此方法接受一个强制参数other ,它指定要比较的时间。

返回值:如果日期在传递的日期之前返回true,否则返回false。

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

程序 1:

// Java program to demonstrate the isBefore() method
import java.time.OffsetTime;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Parses the time
        OffsetTime time1
            = OffsetTime.parse("15:30:30+07:00");
  
        // Parses the time
        OffsetTime time2
            = OffsetTime.parse("15:20:30+07:00");
  
        // gets the time1
        System.out.println("time1: "
                           + time1);
  
        // gets the time2
        System.out.println("time1: "
                           + time2);
  
        System.out.println("time1 is before time2: "
                           + time1.isBefore(time2));
    }
}
输出:
time1: 15:30:30+07:00
time1: 15:20:30+07:00
time1 is before time2: false

方案二

// Java program to demonstrate the isBefore() method
  
import java.time.OffsetTime;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Parses the time
        OffsetTime time1
            = OffsetTime.parse("15:30:30+07:00");
  
        // Parses the time
        OffsetTime time2
            = OffsetTime.parse("15:30:30+07:00");
  
        // gets the time1
        System.out.println("time1: "
                           + time1);
  
        // gets the time2
        System.out.println("time1: "
                           + time2);
  
        System.out.println("time1 is before time2: "
                           + time1.isBefore(time2));
    }
}
输出:
time1: 15:30:30+07:00
time1: 15:30:30+07:00
time1 is before time2: false

参考:https: Java/time/OffsetTime.html#isBefore-java.time.OffsetTime-