Java程序检查两个日期是否相等
让我们首先声明LocalDate的对象-
日期1
LocalDate dateOne = LocalDate.now();
日期2
LocalDate dateTwo = LocalDate.of(dateOne.getYear(), dateOne.getMonth(), dateOne.getDayOfMonth());
现在让我们使用equals()方法检查两个日期是否相同-
if (dateOne.equals(dateTwo)) {
System.out.printf("\nBoth the dates are same!", dateOne, dateTwo);
}示例
import java.time.LocalDate;
public class Demo {
public static void main(String[] argv) {
LocalDate dateOne = LocalDate.now();
LocalDate dateTwo = LocalDate.of(dateOne.getYear(), dateOne.getMonth(), dateOne.getDayOfMonth());
System.out.printf("Date One = "+dateOne);
System.out.printf("\nDate Two = "+dateTwo);
if (dateOne.equals(dateTwo)) {
System.out.printf("\nBoth the dates are same!", dateOne, dateTwo);
}
}
}输出结果
Date One = 2019-04-12 Date Two = 2019-04-12 Both the dates are same!
热门推荐
10 毛笔哥哥结婚祝福语简短
11 向国庆送祝福语简短
12 建队节祝福语简短
13 朋友喜得外孙简短祝福语
14 小店营业的祝福语简短
15 餐饮生日祝福语简短独特
16 鲜花英语祝福语卡片简短
17 男朋友暴富祝福语简短
18 婶婶拜年祝福语大全简短