파싱 parsing format()
날짜와 시간을 원하는 형식으로 출력하고 해석하는것.
java.time.format패키지중 DateTimeFormatter가 핵심 클래스.
상수로 정의된 여러 형식이 있다.
ISO_DATE_TIME
ISO 8601 형식의 날짜 및 시간
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
String formattedDateTime = now.format(formatter);
System.out.println("ISO_DATE_TIME: " + formattedDateTime);
//ISO_DATE_TIME: 2024-02-05T14:30:45.123456789
ISO_LOCAL_DATE
ISO 8601 형식의 날짜, 시간 정보는 포함하지 않는ㄷ.
LocalDate now = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE;
String formattedDate = now.format(formatter);
System.out.println("ISO_LOCAL_DATE: " + formattedDate);
//ISO_LOCAL_DATE: 2024-02-05
ISO_LOCAL_TIME
ISO 8601 형식의 시간, 날짜 정보는 포함하지 않습니다.
LocalTime now = LocalTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_TIME;
String formattedTime = now.format(formatter);
System.out.println("ISO_LOCAL_TIME: " + formattedTime);
// ISO_LOCAL_TIME: 14:30:45.123456789
ISO_LOCAL_DATE_TIME
ISO 8601 형식의 날짜 및 시간을 나타냅니다. 시간대 정보는 포함되지 않습니다.
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
String formattedDateTime = now.format(formatter);
System.out.println("ISO_LOCAL_DATE_TIME: " + formattedDateTime);
// ISO_LOCAL_DATE_TIME: 2024-02-05T14:30:45.123456789
ISO_OFFSET_DATE
ISO 8601 형식의 날짜를 나타내며, 시간대 오프셋을 포함합니다.
OffsetDateTime now = OffsetDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE;
String formattedDateTime = now.format(formatter);
System.out.println("ISO_OFFSET_DATE: " + formattedDateTime);
//ISO_OFFSET_DATE: 2024-02-05+09:00
ISO_OFFSET_TIME
ISO 8601 형식의 시간을 나타내며, 시간대 오프셋을 포함합니다.
OffsetTime now = OffsetTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_TIME;
String formattedTime = now.format(formatter);
System.out.println("ISO_OFFSET_TIME: " + formattedTime);
//ISO_OFFSET_TIME: 14:30:45.123456789+09:00
ISO_OFFSET_DATE_TIME
ISO 8601 형식의 날짜 및 시간을 나타내며, 시간대 오프셋을 포함합니다.
OffsetDateTime now = OffsetDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
String formattedDateTime = now.format(formatter);
System.out.println("ISO_OFFSET_DATE_TIME: " + formattedDateTime);
//ISO_OFFSET_DATE_TIME: 2024-02-05T14:30:45.123456789+09:00
ISO_ZONED_DATE_TIME
ISO 8601 형식의 날짜 및 시간을 나타내며, 시간대 정보를 포함합니다.
ZonedDateTime now = ZonedDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;
String formattedDateTime = now.format(formatter);
System.out.println("ISO_ZONED_DATE_TIME: " + formattedDateTime);
// ISO_ZONED_DATE_TIME: 2024-02-05T14:30:45.123456789+09:00[Asia/Seoul]
ISO_INSTANT
ISO 8601 형식의 날짜와 시간을 UTC 기준으로 나타냅니다.
Instant now = Instant.now();
DateTimeFormatter formatter = DateTimeFormatter.ISO_INSTANT;
String formattedDateTime = now.atZone(ZoneOffset.UTC).format(formatter);
System.out.println("ISO_INSTANT: " + formattedDateTime);
//ISO_INSTANT: 2024-02-05T05:30:45.123456789Z
BASIC_ISO_DATE
기본적인 ISO 8601 형식의 날짜를 나타냅니다. (YYYYMMDD)
LocalDate now = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.BASIC_ISO_DATE;
String formattedDate = now.format(formatter);
System.out.println("BASIC_ISO_DATE: " + formattedDate);
//BASIC_ISO_DATE: 20240205
ISO_DATE
ISO 8601 형식의 날짜를 나타냅니다. 시간대 정보는 포함되지 않습니다.
LocalDate now = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE;
String formattedDate = now.format(formatter);
System.out.println("ISO_DATE: " + formattedDate);
//ISO_DATE: 2024-02-05
ISO_TIME
ISO 8601 형식의 시간을 나타냅니다. 날짜 정보는 포함되지 않습니다.
LocalTime now = LocalTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ISO_TIME;
String formattedTime = now.format(formatter);
System.out.println("ISO_TIME: " + formattedTime);
//ISO_TIME: 14:30:45.123456789
ISO_ORDINAL_DATE
ISO 8601 형식의 연중 날짜를 나타냅니다. (YYYY-DDD)
LocalDate now = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ISO_ORDINAL_DATE;
String formattedDate = now.format(formatter);
System.out.println("ISO_ORDINAL_DATE: " + formattedDate);
// ISO_ORDINAL_DATE: 2024-036
ISO_WEEK_DATE
ISO 8601 형식의 연중 주를 나타냅니다. (YYYY-Www-D)
LocalDate now = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ISO_WEEK_DATE;
String formattedDate = now.format(formatter);
System.out.println("ISO_WEEK_DATE: " + formattedDate);
// ISO_WEEK_DATE: 2024-W06-2
RFC_1123_DATE_TIME
RFC 1123 형식의 날짜 및 시간을 나타냅니다.
ZonedDateTime now = ZonedDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.RFC_1123_DATE_TIME.withLocale(Locale.ENGLISH);
String formattedDateTime = now.format(formatter);
System.out.println("RFC_1123_DATE_TIME: " + formattedDateTime);
// RFC_1123_DATE_TIME: Sun, 05 Feb 2024 14:30:45 +0900
DateTimeFormatter의 static메서드 ofLocalizedDate(), ofLocalizedTime(), ofLocalized DateTime()은
locale에 종속적인 포맷터를 생성한다.
locale은 FULL, LONG, MEDIUM, SHORT가 있다.
| FULL | 2015년 11월 28일 토요일 | N/A |
| LONG | 2015년 11월 28일 (토) | 오후 9시 15분 13초 |
| MEDIUM | 2015.11.28 | 오후 9:15:13 |
| SHORT | 15.11.28 | 오후 9:15 |
ofLocalizedDate()
주어진 로케일에 맞는 형식화된 날짜 포맷터를 생성합니다.
LocalDate now = LocalDate.now();
Locale locale = Locale.getDefault(); // 기본 로케일 사용
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)
.withLocale(locale);
String formattedDate = now.format(formatter);
System.out.println("Formatted Date: " + formattedDate);
// Formatted Date: Monday, February 5, 2024
ofLocalizedTime()
주어진 로케일에 맞는 형식화된 시간 포맷터를 생성합니다.
LocalTime now = LocalTime.now();
Locale locale = Locale.getDefault(); // 기본 로케일 사용
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)
.withLocale(locale);
String formattedTime = now.format(formatter);
System.out.println("Formatted Time: " + formattedTime);
// Formatted Time: 14:30
ofLocalizedDateTime()
주어진 로케일에 맞는 형식화된 날짜 및 시간 포맷터를 생성합니다.
LocalDateTime now = LocalDateTime.now();
Locale locale = Locale.getDefault(); // 기본 로케일 사용
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)
.withLocale(locale);
String formattedDateTime = now.format(formatter);
System.out.println("Formatted Date Time: " + formattedDateTime);
// Formatted Date Time: Feb 5, 2024, 2:30:45 PM
DateTimeFormatter의 ofPattern()으로 원하는 출력형식을 직접 작성 할 수 도 있다.
출력형식에 사용되는 기호는 앞서 살펴본 SimpleDateFormat와 동일하다.
LocalDateTime now = LocalDateTime.now();
// 원하는 출력 형식을 직접 지정
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 지정된 형식으로 날짜 및 시간을 형식화
String formattedDateTime = now.format(formatter);
System.out.println("Formatted Date Time: " + formattedDateTime);
//Formatted Date Time: 2024-02-05 14:30:45
https://standout.tistory.com/1226
SimpleDateFormat, Date와 Calendar보다 쉽게 날짜 형식 지정하기
SimpleDateFormat Date와 Calendar만으로 날짜 데이터를 출력하는 것은 불편하다. SimpleDateFormat를 이용시 여러 문제들이 간단히 해결된다. 편의상 전체를 통합한 예시를 첨부했으나 사용시에는 선택적으
standout.tistory.com
'JAVA' 카테고리의 다른 글
| 키과 값을 하나의 쌍으로 묵어서 저장하는 컬렉션 클래스, Map (0) | 2024.02.05 |
|---|---|
| 중복을 허용하면서 저장순서가 유지되는 List인터페이스 (0) | 2024.02.05 |
| Period와 Duration 날짜/시간의 차이 사칙연산, 유용한 메서드 (0) | 2024.02.02 |
| TemporalAdjusters, 자주쓰일만한 날짜 계산을 대신 해주다. (0) | 2024.02.02 |
| LocalDateTime, ZonedDateTime: now() of() 날짜/시간지정, ZonedId 특정지역, ZoneOffset 일관된 시간대를 구하다 (0) | 2024.01.12 |