「Java8」isBefore(isAfter)メソットとequalsメソットで同一日付を比較するサンプル
説明
public boolean equals(Object obj)
この時間がもう一方の時間と等しいかどうかを確認します。
Javaコード
package com.arkgame.study.utils; import java.time.LocalDate; public class LocalDateCompareEqual { public static void main(String[] args) { LocalDate localDateA = LocalDate.of(2020, 10, 28); LocalDate localDateB = LocalDate.of(2020, 10, 28); boolean resultA; boolean resultB; System.out.println("************同一日比較(isBefore equals)**********"); // isBefore resultA = localDateA.isBefore(localDateB); System.out.println("日付A [" + localDateA + "]と日付B [" + localDateB + "]の比較(equals no use): " + resultA); // isBefore resultA = localDateA.isBefore(localDateB) || localDateA.equals(localDateB); System.out.println("日付A [" + localDateA + "]と日付B [" + localDateB + "]の比較(equals use): " + resultA); System.out.println(); System.out.println("************同一日比較(isAfter equals)***********"); // isAfter resultB = localDateA.isBefore(localDateB); System.out.println("日付A [" + localDateA + "]と日付B [" + localDateB + "]の比較(equals no use): " + resultB); // isAfter resultB = localDateA.isBefore(localDateB) || localDateA.equals(localDateB); System.out.println("日付A [" + localDateA + "]と日付B [" + localDateB + "]の比較(equals use): " + resultB); } }
実行結果
************同一日比較(isBefore equals)**********
日付A [2020-10-28]と日付B [2020-10-28]の比較(equals no use): false
日付A [2020-10-28]と日付B [2020-10-28]の比較(equals use): true
************同一日比較(isAfter equals)***********
日付A [2020-10-28]と日付B [2020-10-28]の比較(equals no use): false
日付A [2020-10-28]と日付B [2020-10-28]の比較(equals use): true