Java中的MonthDay compareTo()方法
可以使用compareTo()Java中MonthDay类中的方法比较两个MonthDay对象。此方法需要单个参数,即要比较的MonthDay对象。
如果第一个MonthDay对象大于第二个MonthDay对象,则返回正数;如果第一个MonthDay对象小于第二个MonthDay对象,则返回负数;如果两个MonthDay对象相等,则返回零。
演示此的程序如下
示例
import java.time.*;
public class Main {
public static void main(String[] args) {
MonthDay md1 = MonthDay.parse("--02-22");
MonthDay md2 = MonthDay.parse("--05-15");
System.out.println("The first MonthDay object is: " + md1);
System.out.println("The second MonthDay object is: " + md2);
int val = md1.compareTo(md2);
if(val > 0)
System.out.println("\nThe first MonthDay object is greater than the second MonthDay object");
else if(val < 0)
System.out.println("\nThe first MonthDay object is lesser than the second MonthDay object");
else
System.out.println("\nThe MonthDay objects are equal");
}
}输出结果
The first MonthDay object is: --02-22 The second MonthDay object is: --05-15 The first MonthDay object is lesser than the second MonthDay object
现在让我们了解上面的程序。
首先显示两个MonthDay对象。然后使用compareTo()方法比较它们,并使用ifelse语句显示结果。演示此代码段如下所示:
MonthDay md1 = MonthDay.parse("--02-22");
MonthDay md2 = MonthDay.parse("--05-15");
System.out.println("The first MonthDay object is: " + md1);
System.out.println("The second MonthDay object is: " + md2);
int val = md1.compareTo(md2);
if(val > 0)
System.out.println("\nThe first MonthDay object is greater than the second MonthDay object");
else if(val < 0)
System.out.println("\nThe first MonthDay object is lesser than the second MonthDay object");
else
System.out.println("\nThe MonthDay objects are equal");热门推荐
1 狗狗技能训练
5 如何训练狗狗笑
6 狗狗训练排便
9 狗狗响片训练
10 如何训练狗狗不捡食
11 狗狗尿训练
12 怎么训练狗狗找东西
13 狗狗训练站立
14 狗狗训练飞盘
15 鞍山狗狗训练
16 怎样训练泰迪狗狗谢谢
17 狗狗外出训练
18 怎么训练金毛狗狗大小便