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

LocalDateTime 시간대 정보인 ZoneID와 합쳐진 것이 ZonedDateTime 이다.

ZonedDateTime 필드 구조

public final class ZonedDateTime ... {
    ...
    private final LocalDateTime dateTime;
    private final ZoneOffset offset;
    private final ZoneId zone;
    ...
}
  • ZonedDateTime 의 시간 정보는 LocalDateTime 타입을 사용한다.
  • ZoneOffset 타입은 +09:00 와 같은 오프셋 시간을 갖고 있다.
  • ZoneId 타입은 "Asia/Seoul" 타임존을 갖고 있다.
    • ZoneId로 오프셋을 구할 수 있으나 복잡하여 만들어졌다.

now - 현재 시간 구하기

ZonedDateTime noeZdt = ZonedDateTime.now();
System.out.println("국제표준 현재 시간: " + noeZdt);

국제표준 현재 시간: 2025-06-20T18:09:53.651233400+09:00[Asia/Seoul]


of - 특정 시간대로 표시하고

ZonedDateTime ofZdt = ZonedDateTime.of(2022,1,1,23,59,59,11111,ZoneId.of("Asia/Seoul"));
System.out.println("국제표준 특정 시간: " + ofZdt);

국제표준 특정 시간: 2022-01-01T23:59:59.000011111+09:00[Asia/Seoul]


of - 오버로딩 인자로 LocalDateTime 사용하기

LocalDateTime ldt = LocalDateTime.of(2022,1,1,23,59,59,11111);
ZonedDateTime ofZdt = ZonedDateTime.of(ldt,ZoneId.of("Asia/Seoul"));
System.out.println("국제표준 특정 시간: " + ofZdt);

국제표준 특정 시간: 2022-01-01T23:59:59.000011111+09:00[Asia/Seoul]


withZoneSameInstant - 타임존 변경

LocalDateTime ldt = LocalDateTime.of(2022,1,1,23,59,59,11111);
ZonedDateTime ofZdt = ZonedDateTime.of(ldt,ZoneId.of("Asia/Seoul"));
System.out.println("국제 표준 특정 시간: " + ofZdt);

ZonedDateTime toUtcZdt = ofZdt.withZoneSameInstant(ZoneId.of("UTC"));
System.out.println("국제 표준 시간 Seoul+09 -> UTC+00: " + toUtcZdt);

국제 표준 특정 시간: 2022-01-01T23:59:59.000011111+09:00[Asia/Seoul]
국제 표준 시간 Seoul+09 -> UTC+00: 2022-01-01T14:59:59.000011111Z[UTC]

Asia/Seoul > UTC 변경 시 9시간이 낮아진 것을 볼 수 있다.