Instant 

Instant 클래스는 앞에서 봤던 LocalDate, LocalTime, LocalDateTime 과 달리

기계 관점에서 날짜와 시간을 표현하기 위해 사용합니다.

기계 관점에서는 사람과 달리 날짜와 시간을 구분하기 보다는 하나의 수를 사용하는 것이

계산에 용이하기 때문에 유닉스 에포크 시간(unix epoch time) 1970년 1월 1일 0시 0분 0초 UTC를 기준으로

특정 지점까지 시간을 초로 표현합니다.

팩토리 메서드 ofEpochSecond에 초를 넘겨서 클래스 인스턴스를 생성할수 있으며, 나노초의 정밀도를 제공합니다.

다음 예제를 통해 사용 예시를 확인해보겠습니다.

 

import java.time.*;

public class Main {
	public static void main(String[] args) {
 		Instant i3 = Instant.ofEpochSecond(3);
		Instant i4 = Instant.ofEpochSecond(3, 1_000_000_000);
		Instant i2 = Instant.ofEpochSecond(3, -1_000_000_000);
		
		System.out.println(i3);
		System.out.println(i4);
		System.out.println(i2);   
    }
}

출력 값

1970-01-01T00:00:03Z
1970-01-01T00:00:04Z
1970-01-01T00:00:02Z

 

Duration

두 시간의 객체 사이의 지속 시간을 만들고 표현할수 있습니다.

나노초, 초 단위로 출력이 가능하게 메서드를 제공합니다.

두 개의 LocalTime, LocalDateTime, Instant을 Duration으로 만들 수있습니다.

다음 예제를 통해서 확인해보겠습니다.

 

import java.time.*;
import java.time.temporal.ChronoUnit;

public class Main {
	public static void main(String[] args) {
		LocalTime t1 = LocalTime.of(15, 05, 10);
		LocalTime t2 = LocalTime.of(16, 05, 10);
		
		LocalDateTime dateTime1 = LocalDateTime.of(2022, 12, 1, 12, 00, 00);
		LocalDateTime dateTime2 = LocalDateTime.of(2022, 12, 2, 13, 00, 00);
		
		Duration duration1 = Duration.between(t1, t2);
		Duration duration2 = Duration.between(dateTime1, dateTime2);
		
		Duration fiveMin = Duration.ofMinutes(5);
		Duration sixMin = Duration.of(6, ChronoUnit.MINUTES);
		
		System.out.println(duration1.getSeconds());
		System.out.println(duration2.getSeconds());
		System.out.println(fiveMin);
		System.out.println(sixMin);
    }
}

출력 값

3600
90000
PT5M
PT6M

 

Period

두 시간의 객체 사이의 지속 시간을 만들고 표현할수 있습니다.

년/월/일 단위로 나타냅니다.

Duration의 경우 최대 제어 가능한 단위는 "일" 입니다.

다음 예제를 통해 사용 예시를 확인해보겠습니다.

 

import java.time.*;
import java.time.temporal.ChronoUnit;

public class Main {
	public static void main(String[] args) {
		Period fourDays = Period.ofDays(4);
		Period TwoWeeks = Period.ofWeeks(2);
		Period oneYearTwoMonthsThreeDays = Period.of(1, 2, 3);
		
		System.out.println(fourDays.get(ChronoUnit.DAYS));
		System.out.println(TwoWeeks.get(ChronoUnit.DAYS));
		System.out.println(oneYearTwoMonthsThreeDays.get(ChronoUnit.YEARS));
		System.out.println(oneYearTwoMonthsThreeDays.get(ChronoUnit.MONTHS));
		System.out.println(oneYearTwoMonthsThreeDays.get(ChronoUnit.DAYS));
		
		Period fiveDays = fourDays.plus(Period.ofDays(1));
		System.out.println(fiveDays.get(ChronoUnit.DAYS));
		
		Period threeDays = fourDays.minus(Period.ofDays(1));
		System.out.println(threeDays.get(ChronoUnit.DAYS));
    }
}

출력 값

 

4
14
1
2
3
5
3

 

Duration과 Period 공통 제공 메서드

메서드 정적 메서드 여부 설명
from Y 주어진 Temporal 객체를 이용하여 클래스 인스텉스 생성.
of Y 주어진 구성 요소로 Temporal 객체의
인스턴스 생성.
now Y
시스템 시계로 Temporal 객체 생성.
parse Y 문자열 파싱하여 Temporal 객체 생성.
atOffset N 시간대 오프셋과 Temporal 객체 합침.
atZone N 시간대 오프셋과 Temporal 객체를 합침
format N 지정된 포매터를 이용하여 Temporal
객체를 문자열로 변환.
get N Temporal 객체 상태를 읽어서 반환.
minus N 특정 시간을 뺀 Temporal 객체의 
복사본을 생성.
plus N 특정 시간을 더한 Temporal 객체의 
복사본을 생성.
with N 일부 상태를 바꾼 Temporal 객체의 
복사본을 생성.

 

ChronoUnit

표준 날짜 기간 단위 집합.

이 단위 세트는 날짜, 시간 또는 날짜-시간을 조작하기위한 단위 기반 액세스를 제공합니다. 

 

 

'Java > Time' 카테고리의 다른 글

ZoneId, ZoneDateTime, ZoneOffset  (0) 2022.11.28
TemporalAdjusters와 DateTimeFormatter  (0) 2022.11.28
LocalDate, LocalTime, LocalDateTime  (0) 2022.11.28

+ Recent posts