패키지
java.time
- 날짜와 시간 저장
LocalDate.now()
LocalTime.now()
LocalDateTime.now()
LocalDate.of()
LocalTime.of()
import java.time.*;
LocalDate date = LocalDate.now(); // 2022-09-29
LocalTime time = LocalTime.now(); // 09:30:28.769932500
LocalDateTime dateTime = LocalDateTime.now(); // 2022-09-29T09:30:28.769932500
LocalDate pastDate = LocalDate.of(2022, 01, 01); // 2022-01-01
LocalTime pastDate = LocalTime.of(22, 50, 10); // 22:50:10
now()는 현재 날짜와 시간, of()는 지정한 시간과 날짜를 저장한다.
- 날짜와 시간 형식 수정 ( Time -> String )
ofPattern()
.format()
import java.time.*;
import java.time.format.DateTimeFormatter;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy년 MM월 dd일");
String todayDateTime = formatter.format(LocalDate.now()); // 2022년 09월 29일
String yesterdayDateTime = formatter.format(LocalDate.of(2022,9,28)); //2022년 09월 28일
- String 을 LocalDateTime으로 바꾸기 ( String -> Time )
parse()
import java.time.*;
import java.time.format.DateTimeFormatter;
LocalDateTime myDateTime = LocalDateTime.parse("2022-09-01 22:50:09",
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println(myDateTime); // 2022-09-01T22:50:09
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy년 MM월 dd일");
LocalDate myDate = LocalDate.parse("2022년 09월 01일", formatter);
System.out.println(myDate); // 2022-09-01
날짜밖에 없는데 LocalTimeDate를 사용하면 에러가 난다
- 영어로 월, 요일 받아오기
getDayOfWeek()
getMonth()
import java.time.*;
String getDayOfWeek = LocalDate.of(2022,9, 29).getDayOfWeek().toString(); //THURSDAY
String month = String.valueOf(LocalDate.of(2022,9, 29).getMonth()); //SEPTEMBER
getMonthValue() 는 정수값으로 저장한다.
- 시간 차이 계산
until()
import java.time.*;
import java.time.temporal.ChronoUnit;
LocalDate today = LocalDate.now();
LocalDate lastYear = LocalDate.of(2021, 9, 29);
long diffDay = today.until(lastYear, ChronoUnit.DAYS); //-365
long diffMonth = today.until(lastYear, ChronoUnit.MONTHS); // -12
참고: https://www.baeldung.com/java-date-difference
- 코드 실행 시간 측정
Instant.now()
import java.time.*;
long startTime = Instant.now().toEpochMilli(); // 1ms = 0.001s
String getDayOfWeek = LocalDate.of(2022,9, 29).getDayOfWeek().toString().substring(0,3);
String month = String.valueOf(LocalDate.of(2022,9, 29).getMonth());
System.out.println(getDayOfWeek +" "+ month); //THU SEPTEMBER
long endTime = Instant.now().toEpochMilli();
long timeElapsed = endTime - startTime;
System.out.printf("timeElapsed: %d (ms)", timeElapsed); // timeElapsed: 14 (ms)
공식문서
https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html
https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
관련문제
https://programmers.co.kr/learn/courses/30/lessons/12901
깃허브
https://github.com/Suyoung225/JavaPrac/blob/main/programmers/1.%202016%EB%85%84.md
728x90
'JAVA' 카테고리의 다른 글
[JAVA] 싱글톤 패턴 정리 (0) | 2022.10.05 |
---|---|
[JAVA] JVM이란? 개념 및 구조 (0) | 2022.10.02 |
[JAVA] Collection - Stack, Queue, ArrayDeque (0) | 2022.09.28 |
[Java] 진수변환(10진수 <->2,8,16진수) (0) | 2022.09.27 |
정규 표현식 Regular expressions (Regex) (1) | 2022.09.25 |
댓글