Java中的 YearMonth isBefore() 方法及示例
Java中 Year 类的isBefore()方法用于检查当前 YearMonth 对象是否在指定为此方法参数的 YearMonth 之前。
语法:
public boolean isBefore(Year otherYearMonth)
参数:它接受一个参数otherYearMonth与当前 YearMonth 对象进行比较。
返回值:如果此 YearMonth 对象的值在指定为方法参数的 YearMonth 对象的值之前,则返回布尔True 值,否则返回 False。
下面的程序说明了Java中的 YearMonth.isBefore() 方法:
程序 1 :
// Program to illustrate the isBefore() method
import java.util.*;
import java.time.*;
public class GfG {
public static void main(String[] args)
{
// Create first Year object
YearMonth yearMonth1
= YearMonth.of(2018, 3);
// Create second Year object
YearMonth yearMonth2
= YearMonth.of(1997, 4);
// Check if this year-month object's value is
// before the specified Year-month or not
System.out.println(yearMonth1
.isBefore(yearMonth2));
}
}
输出:
false
方案二:
// Program to illustrate the isBefore() method
import java.util.*;
import java.time.*;
public class GfG {
public static void main(String[] args)
{
// Create first Year object
YearMonth yearMonth1
= YearMonth.of(1997, 3);
// Create second Year object
YearMonth yearMonth2
= YearMonth.of(2018, 4);
// Check if this year-month object's value is
// before the specified Year-month or not
System.out.println(yearMonth1
.isBefore(yearMonth2));
}
}
输出:
true
参考:https: Java/time/YearMonth.html#isBefore-java.time.YearMonth-