Java MonthDay isBefore()方法及示例
MonthDay 类的 isBefore() 方法用于检查这个MonthDay是否在作为参数传递的MonthDay之前。该方法返回一个布尔值,显示相同的内容。
语法
public boolean isBefore(MonthDay other)
参数: 该方法接受一个参数 , 即另一个要比较的月日。
返回值: 如果这个月日在指定的月日之前,该方法返回真,否则返回假。
以下程序说明了isBefore()方法:
程序1 :
// Java program to demonstrate// MonthDay.isBefore() method import java.time.*; public class GFG { public static void main(String[] args) { // create a MonthDay object MonthDay month = MonthDay.parse("--10-12"); // create other MonthDay object MonthDay othermonth = MonthDay.parse("--11-12"); // apply isBefore() method boolean value = month.isBefore(othermonth); // print instance System.out.println("monthday:" + month + " is before monthday:" + othermonth + " = " + value); }}
输出。
monthday:--10-12 is before monthday:--11-12 = true
程序2
// Java program to demonstrate// MonthDay.isBefore() method import java.time.*; public class GFG { public static void main(String[] args) { // create a MonthDay object MonthDay month = MonthDay.parse("--10-12"); // create other MonthDay object MonthDay othermonth = MonthDay.parse("--09-12"); // apply isBefore() method boolean value = month.isBefore(othermonth); // print instance System.out.println("monthday:" + month + " is before monthday:" + othermonth + " = " + value); }}输出。
monthday:--10-12 is before monthday:--09-12 = false
**参考资料: ** https://docs.oracle.com/javase/10/docs/api/java/time/MonthDay.html#isBefore(java.time.MonthDay)
