Java Date - Duration, Period

그래프에서 시간을 표현할 때 두 가지 방법이 있다.
참고 삼아 알아두자.

시간의 개념 표현 방법 두 가지

  • 특정 시점의 시간(시각)
    • 프로젝트는 2026년 1월 1일까지 완료해야한다.
    • 다음 회의는 10시부터이다.
  • 시간 사이의 간격
    • 앞으로 2년은 공부해야한다.
    • 프로젝트 기간은 1년 남았다.

Duration, Period

Duration, Period 시간의 간격을 표시한다.
시간의 간격을 영어로 amount of time(시간의 양) 이라 부르기도 하다.

Duration

두 시간 사이의 간격을 시 분 초(나노) 단위로 표시한다.

  • 라면 끊는데 3분 걸려
  • 서울 까지 전주까지는 3시간 걸린다.

Period

두 날짜 사이의 간격을 년, 월 일 단위로 나타낸다.

  • 프로젝트는 2개월 남았다.
  • 기념일이 30일 남았어
  • 프로젝트 시작일과 종료일 사이의 간격을 나타냄 (프로젝트 기간)

Period Duration
단위 년, 월, 일 시간, 분, 초, 나노초
대상 날짜 시간
메소드 getYears(), getMonths(), getDays() toHours(), toMinutes(), getSeconds(), getNano()

참고로 메소드를 살펴보면 접미미사가 to, get 으로 나눠져 있다.

  • get 속성을 바로 반환한다.
  • to 계산해서 반환한다. (초 -> 시)

Duration, Peiod 클래스 멤버 필드

Period

public final class Period ... {
    private final int years;
    private final int months;
    private final int days;
}

년, 월, 일로 구성되어 있다.

Duration

public final class Duration ... {
    private final long seconds;
    private final int nanos;
}

초 또는 나누초로 되어있다.


Period 클래스

ofDays - 10일 생성하기

Period period = Period.ofDays(10);
System.out.println(period);

P10D

toString() 메소드에 의해서 P10D 으로 표기하고 있다.


LocalDate.plus - Period 시간의 양만큼 날짜 계산하기

Period period = Period.ofDays(10);
System.out.println(period);

LocalDate current = LocalDate.of(2025,1,1);
LocalDate periodPlus = current.plus(period);
System.out.println(periodPlus);

P10D
2025-01-11


between - 날짜와 날짜 사이의 기간 반환받기

LocalDate start = LocalDate.of(2023,1,1);
System.out.println("프로젝트 시작날짜: " + start);
LocalDate end = LocalDate.of(2024,5,5);
System.out.println("프로젝트 종료날짜: " + end);
Period between = Period.between(start, end);
System.out.println("두 날짜 사이의 간격: " + between.getYears() + "년, " + between.getMonths() + "월, " + between.getDays() + "일");
System.out.println(between);

프로젝트 시작날짜: 2023-01-01
프로젝트 종료날짜: 2024-05-05
두 날짜 사이의 간격: 1년, 4월, 4일
P1Y4M4D


Duration 클래스

ofHours, ofMinutes, ofSeconds - 시 분 초 반환하기

Duration hours = Duration.ofHours(1);
System.out.println("시 " + hours);
Duration minutes = Duration.ofMinutes(20);
System.out.println("분 " + minutes);
Duration seconds = Duration.ofSeconds(30);
System.out.println("초 " + seconds);

시 PT1H
분 PT20M
초 PT30S


LocalTime.plus - 시간을 더하는데 사용한다.

Duration minutes = Duration.ofMinutes(20);
        
LocalTime lt = LocalTime.of(10, 0);
System.out.println("lt: " + lt);
LocalTime pt = lt.plus(minutes);
System.out.println("pt: " + pt);

lt: 10:00
pt: 10:20

20분이 추가된 것을 볼 수 있다.


Between - 두 시간 차이 표시하기

LocalTime start = LocalTime.of(10,0);
LocalTime end = LocalTime.of(11,30);
Duration between = Duration.between(start, end);
System.out.println("두 시간 사이의 차이(getSeconds): " + between.getSeconds() + "초");
System.out.println("두 시간 사이의 차이(toHours, toMinutesPart): " + between.toHours() + "시" + between.toMinutesPart() + "분");
System.out.println("두 시간 사이의 차이(toHours, toMinutes): " + between.toHours() + "시" + between.toMinutes() + "분");

두 시간 사이의 차이(getSeconds): 5400초
두 시간 사이의 차이(toHours, toMinutesPart): 1시30분
두 시간 사이의 차이(toHours, toMinutes): 1시90분