Java.Time 패키지

Lee kyu min·2024년 3월 30일

Java study

목록 보기
39/41

날짜 형식
y : 년
M : 월
d : 일
h : 시
m : 분
s : 초

1. LocalDate

  • 날짜 정보 저장

1) now()

컴퓨터의 현재 날짜정보를 저장한 LocalDate객체 리턴

LocalDate ld = LocalDate.now();

2) of()

매개값으로 주어진 날짜 정보를 저장한 LocalDate객체 리턴

LocalDate ld = LocalDate.of(int year, int month, int datOfMonth);

2. LocalTime

  • 시간 정보 저장

1) now()

컴퓨터의 현재 시간정보를 저장한 LocalTime객체 리턴

LocalTime lt = LocalTime.now();

2) of()

매개값으로 주어진 시간 정보를 저장한 LocalTime객체 리턴

LocalTime lt = LocalTime.of(int hour, int minute, int second, int nanoOfSecond);

3. LocalDateTime

  • LocalDate + LocalTime(날짜+시간 정보 저장)

1) now()

컴퓨터의 현재 날짜+시간정보를 저장한 LocalDateTime객체 리턴

LocalDateTime ldt = LocalDateTime.now();

2) of()

매개값으로 주어진 날짜+시간 정보를 저장한 LocalDateTime객체 리턴

LocalDateTime ldt = LocalDateTime.of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond);

4. ZonedDateTime

  • 저장 형태에 타임존(국가별 지역에 따른 시간의 차이)정보가 붙음
    ex) 2024-03-30T17:41:46.156417700+09:00[Asia/Seoul]
    1) now()
    2) of()

ex)

ZonedDateTime utc = ZonedDateTime.now(Zoneld.of("UTC"));
ZonedDateTime london = ZonedDateTime.now(Zoneld.of("Europe/London"));
ZonedDateTime seoul = ZonedDateTime.now(Zoneld.of("Asia/Seoul"));

5. Instant

  • UTC(협정 세계시) 기준 특정 시점의 시간 저장
    타임 스탬프로 사용
    1) now()
Instant i1 = Instant.now();
Instant i2 = Instant.now();

6. 메소드

1) format() : 날짜와 시간 정보를 문자열로 변환

DateTimeFormatter 에서 지정한 형식으로 문자열 리턴

        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy년 M월 d일 h시 m분");
        String snow = now.format(dtf);

        System.out.println(snow);	//2024년 3월 30일 6시 11분

2) 정보 얻기

  • getYear() : 년
  • getMonthValue() : 월
  • getDayOfMonth() : 월의 몇번쨰 일
  • getDayOfWeek() : 요일
  • getHour() : 시간
  • getMinute() : 분
  • getSecond() : 초

0개의 댓글