Back-end/JAVA & Spring
[Java] 현재 날짜, 현재 시간 가져오기 _ Java 8 이후
꼬바리
2022. 7. 11. 16:06
Java 8 이후
- java.time.LocalDate
- java.time.LocalTime
- java.time.LocalDateTime
현재 날짜(타임존 적용) 구하기
import java.time.LocalDate;
import java.time.ZoneId;
public class CurrentDateTime {
public static void main(String[] args) {
// 현재 날짜 구하기 (시스템 시계, 시스템 타임존)
LocalDate now = LocalDate.now();
// 현재 날짜 구하기(Paris)
LocalDate parisNow = LocalDate.now(ZoneId.of("Europe/Paris"));
// 포맷 정의
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// 포맷 적용
String formatedNow = now.format(formatter);
// 결과 출력
System.out.println(now); // 2021-06-17
System.out.println(parisNow); // 2021-06-16
}
}
LocalDate.now();
시스템에 default로 지정된 시간과 타임존을 이용하여 현재 날짜를 가져옵니다.
LocalDate.now(ZoneId.of("Europe/Paris"));
시스템 시계의 날짜를 Europe/Paris의 타임존을 적용하여 가져옵니다.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
now.format(formatter);
DateTimeFormatter 클래스를 이용하여 원하는 형태로 출력할 수 있습니다.
728x90
반응형