기간, 시간의 간격 - Period, Duration

SungMin·2026년 5월 30일

자바 심화 정리

목록 보기
21/34

시간의 간격은 2가지로 표기할 수 있다.

Period

  • 두 날짜 사이의 간격을 년, 월, 일로 표기한다.
  • getYear(), getMonth(), getDays()

Duration

  • 두 날짜 사이의 간격을 시, 분, 초로 표기한다.
  • toHours(), toMinutes(), getSeconds(), getNano()

package time;

import java.time.Duration;
import java.time.LocalTime;

public class DurationMain {
    public static void main(String[] args) {
        Duration duration = Duration.ofMinutes(30);
        System.out.println("duration = " + duration);

        LocalTime lt = LocalTime.of(1, 0);
        System.out.println("lt = " + lt);

        // 계산에 적용
        LocalTime plusTime = lt.plus(duration);
        System.out.println("plusTime = " + plusTime);

        // 시간 차이
        LocalTime start = LocalTime.of(9, 0);
        LocalTime end = LocalTime.of(10, 0);
        Duration between = Duration.between(start, end);
        System.out.println("차이: " + between.getSeconds() + "초");
        System.out.println("근무 시간: " + between.toHours() + "시간 " + between.toMinutesPart() + "분");

    }
}
profile
오늘도 한 걸음씩 나아가

0개의 댓글