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!
热门推荐
2 红丝绒戚风蛋糕
3 夹心小甜点
4 南瓜甜点
6 粘米粉蒸蛋糕
7 酸奶华夫饼
9 红柚鸡蛋糕
10 葡萄干蛋糕
11 紫薯蛋糕卷
13 纸杯裱花小蛋糕
14 果酱奶酪蛋糕
15 日式樱花蔓越莓大福
16 草莓蛋糕卷
17 卡仕达长崎杯子蛋糕