🔻Language/Java

[Java] 두 날짜 사이의 차이 계산하기

_니지 2023. 2. 24. 21:24

두 날짜 사이의 간격을 나타내는 기능을 써보려고 한다!

public class period {
    public static void main(String[] args) {
        LocalDate start = LocalDate.now(); //2023-02-24
        LocalDate end = start.plusDays(5); //2023-03-01

        System.out.println("start: " + start); //2023-02-24
        System.out.println("end: " + end); //2023-03-01

        Period period = Period.between(start, end);
        System.out.println("period.getDays(): "+ period.getDays()); //5
        System.out.println("period.getMonths(): "+ period.getMonths()); //0
        System.out.println("period.getYears(): "+ period.getYears()); //0
    }
}

start와 end를 LocalDate로 생성하고 5일 차이가 나게 설정했다

Period period로 두 LocalDate 사이의 날짜를 객체로 만든 후

period에서 Days, Months, Years를 get으로 가져와 출력할 수 있다!

 

 

728x90
반응형