새소식

반응형
Java

[Java] java8 Time 관련 정리

  • -
반응형

java의 time 관련 표현 방법 정리

java.time package : https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html

 

비교 대상

LocalDateTime, OffsetDateTime, ZonedDateTime

 

LocalDateTime

ISO-8601 캘린더 시스템의 타임존 개념이 없는, 날짜-시간 시스템.

ex : 2023-08-19T09:33:26.590

해당 컴퓨터의 타임존에 맞춘 시간을 의미

서울에서 new LocalDateTime()을 하면 서울의 현재 시각이 나온다

ISO-8601는 날짜와 시간과 관련된 국제표준이다.
ISO-8601의 핵심은 - : T W Z 같은 정해진 문자만 써서 작성한다는 것

ZonedDateTime

ISO-8601 캘린더에 정의된 타임존을 포함한 날짜-시간 시스템

ex : 2023-08-19T09:33:26.591+09:00[Asia/Seoul]

ex 타임존 : +09:00[Asia/Seoul]

 

OffsetDateTime

date-time + offset (UTC/그리니치에서부터의 오프셋만을 표현)

offset : 타임존의 정보에서 존에 대한 정보를 제거한 내역 ( +09:00[Asia/Seoul] 에서 +09:00가 offset )

ex : 2023-08-19T09:33:26.591+09:00

ex offset : +09:00

 

정보의

LocalDateTime < OffsetDateTime < ZonedDateTime

 

example code

        LocalDateTime localDateTime = LocalDateTime.now();
        OffsetDateTime offsetDateTime = OffsetDateTime.now();
        ZonedDateTime zonedDateTime = ZonedDateTime.now();

        System.out.println(localDateTime);
        System.out.println(offsetDateTime);
        System.out.println(zonedDateTime);

        System.out.println(localDateTime.atOffset(ZoneOffset.ofHours(4)));
        System.out.println(localDateTime.atZone(ZoneId.of("Africa/Cairo")));

output 

2023-08-19T09:57:22.117
2023-08-19T09:57:22.118+09:00
2023-08-19T09:57:22.118+09:00[Asia/Seoul]

2023-08-19T09:57:22.117+04:00
2023-08-19T09:57:22.117+02:00[Africa/Cairo]

 

DateTimeFormatter

https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

 

ISO_LOCAL_DATE_TIME - yyyy-MM-dd'T'HH:mm:ss

 

DateTimeFormatter를 이용해 formatter을 맞추고 싶은 경우

yyyy-MM-dd'T'HH:mm:ss -> 2023-08-19T09:57:22.117

yyyy-MM-dd'T'HH:mm:ss.SSSZZ -> 2023-08-19T09:57:22.118+09:00

 

ms뒤에 Zone의 표현 알파벳, 개수에 따라 offset의 표현되는 방식이 다르다

Z :

One, two or three letters outputs the hour and minute, without a colon, such as '+0130'. The output will be '+0000' when the offset is zero. 

2023-08-19T10:48:14.489+0900

 

Four letters outputs the full form of localized offset, equivalent to four letters of Offset-O.

The output will be the corresponding localized offset text if the offset is zero. 

2023-08-19T10:48:14.489GMT+09:00

 

Five letters outputs the hour, minute, with optional second if non-zero, with colon.

It outputs 'Z' if the offset is zero

2023-08-19T10:48:14.489+09:00

 


FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); 
System.out.println(FORMATTER.format(zonedDateTime));
FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"); 
System.out.println(FORMATTER.format(zonedDateTime));
FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"); 
System.out.println(FORMATTER.format(zonedDateTime));
2023-08-19T10:48:14.489+0900
2023-08-19T10:48:14.489GMT+09:00
2023-08-19T10:48:14.489+09:00

 

X:

One letter outputs just the hour, such as '+01',

unless the minute is non-zero in which case the minute is also output, such as '+0130'.

2023-08-19T10:48:14.489+09

 

Two letters outputs the hour and minute, without a colon, such as '+0130'.

2023-08-19T10:48:14.489+0900

 

Three letters outputs the hour and minute, with a colon, such as '+01:30'.

2023-08-19T10:48:14.489+09:00

 

Four letters outputs the hour and minute and optional second, without a colon, such as '+013015'.

 

Five letters outputs the hour and minute and optional second, with a colon, such as '+01:30:15'.

 


FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSx"); 
System.out.println(FORMATTER.format(zonedDateTime));
FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSxx"); 
System.out.println(FORMATTER.format(zonedDateTime));
FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSxxx"); 
System.out.println(FORMATTER.format(zonedDateTime));
FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSxxxx"); 
System.out.println(FORMATTER.format(zonedDateTime));
FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSxxxxx"); 
System.out.println(FORMATTER.format(zonedDateTime));
2023-08-19T10:48:14.489+09
2023-08-19T10:48:14.489+0900
2023-08-19T10:48:14.489+09:00
2023-08-19T10:48:14.489+0900
2023-08-19T10:48:14.489+09:00

 

반응형

'Java' 카테고리의 다른 글

[Java] ForkJoinPool이란 (ParellelStream)  (0) 2023.12.21
[JAVA] Java8 변경 사항 ( java7 to java8)  (0) 2023.11.25
[JAVA] Thread 종류 및 상태  (0) 2023.07.08
[Java] java.security.egd option  (0) 2023.06.28
[Java] ThreadLocal이란  (0) 2022.11.24
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.