LocalDateTime, LocalDate, ZonedDateTime을 이용하여 현재 시간을 가져오는 방법입니다.

1. LocalDateTime, LocalDate의 현재 시간

LocalDateTime, LocalDate은 now() 함수를 제공하며 현재 시간 정보를 갖고 있는 객체를 리턴합니다.

import java.time.LocalDate;
import java.time.LocalDateTime;

public class Example {
    public static void main(String[] args) {

        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);

        LocalDate nowDate = LocalDate.now();
        System.out.println(nowDate);
    }
}

Output:

2022-12-17T06:39:43.780479086
2022-12-17

1.1 객체에서 시간, 날짜 정보 가져오기

LocalDateTime, LocalDate 객체에서 아래와 같이 시간 정보를 가져올 수 있습니다.

  • getYear() : 년 정보 리턴
  • getMonth() : 월 정보를 Month 객체로 리턴
  • getMonthValue() : 월 정보를 Int로 리턴
  • getDayOfMonth() : 일 정보 리턴 (월의 몇번째 날짜)
  • getDayOfYear() : 년의 몇번째 날짜인지 Int로 리턴
  • getDayOfWeek() : 요일 정보를 DayOfWeek 객체로 리턴, Int로 변환하려면 getValue() 호출
  • getHour(), getMinute(), getSecond() : 시/분/초 정보 리턴
import java.time.LocalDateTime;

public class Example {
    public static void main(String[] args) {

        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);

        System.out.println("year: " + now.getYear());
        System.out.println("month: " + now.getMonth());
        System.out.println("month(int): " + now.getMonth().getValue());
        System.out.println("day: " + now.getDayOfMonth());
        System.out.println("day(of year): " + now.getDayOfYear());
        System.out.println("day(of week): " + now.getDayOfWeek());
        System.out.println("day(of week)(int): " + now.getDayOfWeek().getValue());
        System.out.println("hour: " + now.getHour());
        System.out.println("min: " + now.getMinute());
        System.out.println("sec: " + now.getSecond());
    }
}

Output:

2022-12-17T07:01:27.052357183
year: 2022
month: DECEMBER
month(int): 12
day(of year): 351
day(of week): SATURDAY
day(of week)(int): 6
day: 17
hour: 7
min: 1
sec: 27

1.2 특정 시간 객체 가져오기

현재 시간이 아닌 특정 시간의 LocalDate, LocalDateTime 객체를 생성하려면 of()를 이용하여 시간을 지정할 수 있습니다.

import java.time.LocalDate;
import java.time.LocalDateTime;

public class Example {
    public static void main(String[] args) {

        LocalDateTime now = LocalDateTime.of(2022, 1, 10, 9, 15, 30);
        System.out.println(now);

        LocalDate nowDate = LocalDate.of(2022, 2, 15);
        System.out.println(nowDate);
    }
}

Output:

2022-01-10T09:15:30
2022-02-15

2. ZonedDateTime, 특정 지역의 현재 시간

ZonedDateTime는 특정 지역의 시간 정보를 가져옵니다.

  • ZonedDateTime.now() : 시스템에 설정된 지역의 현재 시간을 가져옴
  • ZonedDateTime.now(ZoneId) : ZoneId에 해당하는 특정 지역의 현재 시간을 가져옴

아래 예제 코드는 시스템 지역이 Asia/Seoul인 환경에서 실행되었습니다.

import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Example {
    public static void main(String[] args) {

        ZonedDateTime now = ZonedDateTime.now();
        System.out.println(now);

        ZonedDateTime now2 = ZonedDateTime.now(ZoneId.of("America/Los_Angeles"));
        System.out.println(now2);

        ZonedDateTime now3 = ZonedDateTime.now(ZoneId.of("Asia/Ho_Chi_Minh"));
        System.out.println(now3);
    }
}

Output:

2022-12-17T06:46:38.952887851+09:00[Asia/Seoul]
2022-12-16T13:46:38.954690165-08:00[America/Los_Angeles]
2022-12-17T04:46:38.959400217+07:00[Asia/Ho_Chi_Minh]

2.1 ZoneId

ZoneId는 다음과 같은 것들을 사용할 수 있습니다.

  • Australia/Darwin
  • Australia/Sydney
  • America/Argentina/Buenos_Aires
  • Africa/Cairo
  • America/Anchorage
  • America/Sao_Paulo
  • Asia/Dhaka
  • Africa/Harare
  • America/St_Johns
  • America/Chicago
  • Asia/Shanghai
  • Africa/Addis_Ababa
  • Europe/Paris
  • America/Indiana/Indianapolis
  • Asia/Kolkata
  • Asia/Tokyo
  • Pacific/Apia
  • Asia/Yerevan
  • Pacific/Auckland
  • Asia/Karachi
  • America/Phoenix
  • America/Puerto_Rico
  • America/Los_Angeles
  • Pacific/Guadalcanal
  • Asia/Ho_Chi_Minh