상세 컨텐츠

본문 제목

Duration, Period

Java

by Wanderer Kim 2025. 6. 2. 17:57

본문

728x90

java에서 시간의 간격 및 기간을 나타내는 Duration, Period 클래스에 대해서 알아보자.

Period

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

public class Period {
	private final int years;
	private final int months;
	private final int days;
}
package time;
import java.time.LocalDate;
import java.time.Period;
public class PeriodMain {
	public static void main(String[] args) {
		//생성
		Period period = Period.ofDays(10);
		System.out.println("period = " + period);
		//계산에 사용
		LocalDate currentDate = LocalDate.of(2030, 1, 1);
		LocalDate plusDate = currentDate.plus(period);
		System.out.println("현재 날짜: " + currentDate);
		System.out.println("더한 날짜: " + plusDate);
		//기간 차이
		LocalDate startDate = LocalDate.of(2023, 1, 1);
		LocalDate endDate = LocalDate.of(2023, 4, 2);
		Period between = Period.between(startDate, endDate);
		System.out.println("기간: " + between.getMonths() + "개월 " + between.getDays() + "일");
	}
}
period = P10D
현재 날짜: 2030-01-01
더한 날짜: 2030-01-11
기간: 3개월 1일

생성

  • of(): 특정 기간을 지정해서 Period를 생성한다.
    • of(년,월,일)
    • ofDays()
    • ofMonths()
    • ofYears()

계산에 사용

  • 2030년 1월 1일에 10일을 더하면 2030년 1월 11일이 된다. 라고 표현할 때 특정 날짜에 10일이라는 기간을 더할 수 있다.

기간 차이

  • 2023년 1월 1일과 2023년 4월 2일간의 차이는 3개월 1일이다. 라고 표현할 때 특정 날짜의 차이를 구하면 기간이 된다.
  • Period.betweeb(startDate, endDate)와 같이 특정 날짜의 차이를 구하면 Period가 반환된다.

Duration

두 시간 사이의 간격을 시,분,초 단위로 나타낸다.

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

내부에서 초를 기반으로 시,분,초를 계산해서 사용한다.

  • 1분 = 60초
  • 1시간 = 3600초
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);
		//계산에 사용
		LocalTime plusTime = lt.plus(duration);
		System.out.println("더한 시간 = " + 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() + "분");
	}
}
duration = PT30M
기준 시간 = 01:00
더한 시간 = 01:30
차이: 3600초
근무 시간: 1시간 0분

 

  • 생성
    • of(): 특정 시간을 지정해서 Duration을 생성한다.
      • of(지정)
      • ofSeconds()
      • ofMinutes()
      • ofHours()
  • 계산에 사용
    • 1:00에 30분을 더하면 1:30이 된다. 라고 표현할 때 특정 시간에 30분이라는 시간을 더할 수 있다.
  • 시간 차이
    • 9시와 10시의 차이는 1시간이라고 표현할 때 시간의 차이를 구할 수 있다.
    • Duration.between(start, end)와 같이 특정 시간의 차이를 구하면 Duration이 반환된다.
반응형

'Java' 카테고리의 다른 글

java에서 Thread를 생성하는 방법  (1) 2025.05.26
Instant  (1) 2025.05.23
OffsetDateTime  (0) 2025.05.23
ZonedDateTime  (1) 2025.05.22
Context Switching이란?  (0) 2025.05.16

관련글 더보기

댓글 영역