소개
이 랩에서는 java.time 패키지의 LocalDate 클래스에 있는 isAfter() 메서드를 소개합니다. 이 메서드는 한 날짜가 다른 날짜보다 뒤인지 여부를 나타내는 부울 값을 반환합니다.
이 랩에서는 java.time 패키지의 LocalDate 클래스에 있는 isAfter() 메서드를 소개합니다. 이 메서드는 한 날짜가 다른 날짜보다 뒤인지 여부를 나타내는 부울 값을 반환합니다.
LocalDate 클래스와 isAfter() 메서드를 사용하려면 파일 시작 부분에서 다음 패키지를 임포트해야 합니다.
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
두 개의 서로 다른 날짜를 나타내는 두 개의 LocalDate 객체를 생성합니다. 예를 들어:
LocalDate firstDate = LocalDate.of(2021, 1, 1);
LocalDate secondDate = LocalDate.of(2021, 6, 1);
isAfter() 메서드를 사용하여 firstDate가 secondDate 이후인지 확인합니다. 결과를 콘솔에 출력합니다. 예를 들어:
System.out.println(firstDate + " is after " + secondDate + ": " + firstDate.isAfter(secondDate));
isAfter() 메서드를 사용하여 secondDate가 firstDate 이후인지 확인합니다. 결과를 콘솔에 출력합니다. 예를 들어:
System.out.println(secondDate + " is after " + firstDate + ": " + secondDate.isAfter(firstDate));
현재 날짜와 내일 날짜를 나타내는 두 개의 LocalDate 객체를 생성합니다. now() 메서드와 plus() 메서드를 사용하여 이를 수행할 수 있습니다. 예를 들어:
LocalDate currentDate = LocalDate.now();
LocalDate tomorrowDate = currentDate.plus(1, ChronoUnit.DAYS);
isAfter() 메서드를 사용하여 currentDate가 tomorrowDate 이후인지 확인합니다. 결과를 콘솔에 출력합니다. 예를 들어:
System.out.println(currentDate + " is after " + tomorrowDate + ": " + currentDate.isAfter(tomorrowDate));
isAfter() 메서드를 사용하여 tomorrowDate가 currentDate 이후인지 확인합니다. 결과를 콘솔에 출력합니다. 예를 들어:
System.out.println(tomorrowDate + " is after " + currentDate + ": " + tomorrowDate.isAfter(currentDate));
터미널에서 다음 명령을 사용하여 코드를 컴파일합니다:
javac LocalDateIsAfter.java
터미널에서 다음 명령을 사용하여 코드를 실행합니다:
java LocalDateIsAfter
이 랩에서는 java.time 패키지의 LocalDate 클래스에 있는 isAfter() 메서드에 대해 배웠습니다. 이 메서드를 사용하여 한 날짜가 다른 날짜 이후인지 확인하는 방법을 살펴보았습니다.