Java Date - OffsetDateTime 날짜와 시간 다루기

OffsetDateTime 클래스는 LocalDateTime 과 UTC 오프셋 정보가 담긴 ZoneOffset 이 합쳐진 것이다.

OffsetDateTime 구조의 필드

public final class OffsetDateTime ... {
    ...
    private final LocalDateTime dateTime;
    private final ZoneOffset offset;
    ...
}

ZonedDateTime 클래스와 다른 점은 ZoneId 필드가 없다.
UTC로부터 시간대 차이로 발생한 오프셋 정보를 포함한다.

now - 현재 시간 구하기

OffsetDateTime now = OffsetDateTime.now();
System.out.println("Offset Date Time" + now);

Offset Date Time2025-06-20T18:26:37.700524500+09:00


of - 특정 시간대를 가리켜 날짜와 시간 객체 만들기

LocalDateTime ldt = LocalDateTime.of(2022,2,22,23,59,59);
OffsetDateTime odt = OffsetDateTime.of(ldt, ZoneOffset.of("+01:00"));
System.out.println("가리킨 날짜와 시간: " + odt);

가리킨 날짜와 시간: 2022-02-22T23:59:59+01:00

참고로 of의 인자로 ZoneOffset 주었을 때 "Asia/Seoul"은 지정할 수 없다.


ZonedDateTime vs OffsetDateTime

  • ZonedDateTime 구체적인 시간대를 다룰 때 일광 절약 시간을 자동으로 처리할 수 있어 국제적인 사용자 지정 시간에서 계산이 필요할 때 적합하다.
  • OffsetDateTime UTC와 시간 차이만 나타날 뿐 일광 절약 시간 계산과 같은 복잡성을 다루지 않는다. 시간대 변환없이 로그에 기록하고, 데이터에 처리에 적합하다.