ZonedDateTime
LocalDateTIme, ZonedDateTime은 java.time 패키지의 API로 자바 8 부터 추가되어 날짜 및 시간을 간단히 처리할 수 있습니다. 그중에 ZonedDateTime은 시간대를 정보를 포함하는 객체로 LocalDateTime 보다 다국어 처리에 용의합니다.

▼ 전체 ZoneId 목록을 가져옵니다. ZoneDateTime을 만들기 위해서는 ZoneId 가 필요합니다. 예를 들어 서울의 경우 Asia/Seoul 이 ZoneId가 됩니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void printZone() { | |
Set<String> zone = ZoneId.getAvailableZoneIds(); | |
zone.forEach(i -> { | |
ZonedDateTime now = ZonedDateTime.now(ZoneId.of(i)); | |
System.out.println(i + " : " + now); | |
}); | |
} |
Asia/Aden : 2022-09-29T14:20:41.657715+03:00[Asia/Aden]
America/Cuiaba : 2022-09-29T07:20:41.658806-04:00[America/Cuiaba]
Etc/GMT+9 : 2022-09-29T02:20:41.658854-09:00[Etc/GMT+9]
Etc/GMT+8 : 2022-09-29T03:20:41.658888-08:00[Etc/GMT+8]
...
▼ 다음은 예제는 ZoneDateTime을 객체를 만들어 출력합니다. 단순히 UTC, Asia/Seoul 의 ZoneDateTime 객체를 만들고 출력합니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ZonedDateTime utc = ZonedDateTime.now(ZoneId.of("UTC")); | |
System.out.println(utc); | |
ZonedDateTime seoul = ZonedDateTime.now(ZoneId.of("Asia/Seoul")); | |
System.out.println(seoul); |
2022-09-29T21:53:25.456288+09:00[Asia/Seoul]
2022-09-29T12:53:25.456415Z[UTC]
▼ 이번에는 ZonedDateTime을 LocalDateTime으로 변환하고 다시 LocalDateTime을 ZoneDateTime으로 변환합니다. ZoneId 만 알고 있다면 LocalDateTime을 손쉽게 ZonedDateTime으로 바꿀 수 있습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LocalDateTime localSeoul = seoul.toLocalDateTime(); | |
System.out.println(localSeoul); | |
ZonedDateTime zonedSeoul = localSeoul.atZone(ZoneId.of("Asia/Seoul")); | |
System.out.println(zonedSeoul); |
2022-09-29T21:53:47.172494
2022-09-29T21:53:47.172494+09:00[Asia/Seoul]
▼ 다음 예제는 DateTimeFormatter를 사용해서 ZonedDateTime 객체를 문자열로 변환합니다. 포매터의 형태에 따라 다양한 방식으로 변환이 가능합니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Asia/Seoul")); | |
String nowStr1 = now.format(DateTimeFormatter.ISO_DATE_TIME); | |
String nowStr2 = now.format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss z")); | |
String nowStr3 = now.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL)); | |
String nowStr4 = now.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withLocale(Locale.US)); |
2022-09-29T21:54:35.242084+09:00[Asia/Seoul]
2022/09/29 21:54:35 KST
2022년 9월 29일 목요일 오후 9시 54분 35초 대한민국 표준시
Thursday, September 29, 2022 at 9:54:35 PM Korean Standard Time
▼ 다음 예제는 문자열을 다시 ZoneDateTime 객체로 변환하는 방법입니다. 이 역시 문자열에 맞는 포매터를 생성해서 parse를 통해 역순으로 변환합니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ZonedDateTime now1 = ZonedDateTime.parse(nowStr1); | |
ZonedDateTime now2 = ZonedDateTime.parse(nowStr2, DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss z")); | |
ZonedDateTime now3 = ZonedDateTime.parse(nowStr3, DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL)); | |
ZonedDateTime now4 = ZonedDateTime.parse(nowStr4, DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withLocale(Locale.US)); |
2022-09-29T21:54:35.242084+09:00[Asia/Seoul]
2022-09-29T21:54:35+09:00[Asia/Seoul]
2022-09-29T21:54:35+09:00[Asia/Seoul]
2022-09-29T21:54:35+09:00[Asia/Seoul]
'언어 > Java' 카테고리의 다른 글
[Java] 자바 JAVA_HOME, JDK 설치, 환경변수 설정하기 - 윈도우 10 (0) | 2022.04.14 |
---|---|
[Java] 제네릭 (Generics) 사용법 (0) | 2021.06.14 |
[Java] 자바 스레드 (Thread) 처리 방법 (0) | 2021.06.10 |
[Java] 자바 예외처리 (Exceptions) Try Catch 사용법 (0) | 2021.06.08 |