LocalDateTime 을 사용하는 방법에 대해서 작성한다.

OffsetDateTime과 ZonedDateTime도 같이 사용할 수 있다.

 

1. toLocalDate, toLocalTime, get*

// 현재 날짜 및 시간
LocalDateTime now = LocalDateTime.now();
System.out.println("[now] = " + now);
System.out.println("now.getYear() = " + now.getYear()); // 년
System.out.println("now.getMonth() = " + now.getMonth()); // 월
System.out.println("now.getMonthValue() = " + now.getMonthValue()); // 월(숫자)
System.out.println("now.getDayOfMonth() = " + now.getDayOfMonth()); // 일
System.out.println("now.getHour() = " + now.getHour()); // 시
System.out.println("now.getMinute() = " + now.getMinute()); // 분
System.out.println("now.getSecond() = " + now.getSecond()); // 초 

// 현재 날짜만 빼오기
LocalDate date = now.toLocalDate();
System.out.println("[date] = " + date);
System.out.println("date.getYear() = " + date.getYear()); // 년
System.out.println("date.getMonth() = " + date.getMonth()); // 월
System.out.println("date.getMonthValue() = " + date.getMonthValue()); // 월(숫자)
System.out.println("date.getDayOfMonth() = " + date.getDayOfMonth()); // 일

// 현재 시간만 빼오기
LocalTime time = now.toLocalTime();
System.out.println("[time] = " + time);
System.out.println("time.getHour() = " + time.getHour()); // 시
System.out.println("time.getMinute() = " + time.getMinute()); // 분
System.out.println("time.getSecond() = " + time.getSecond()); // 초
output:

[now] = 2022-05-16T21:33:19.350698
now.getYear() = 2022
now.getMonth() = MAY
now.getMonthValue() = 5
now.getDayOfMonth() = 16
now.getHour() = 21
now.getMinute() = 33
now.getSecond() = 19

[date] = 2022-05-16
date.getYear() = 2022
date.getMonth() = MAY
date.getMonthValue() = 5
date.getDayOfMonth() = 16

[time] = 21:33:19.350698
time.getHour() = 21
time.getMinute() = 33
time.getSecond() = 19

2. 연산(Plus, Minus)

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

// PLUS
System.out.println("now.plusYears(2) = " + now.plusYears(2));
System.out.println("now.plusMonths(5) = " + now.plusMonths(5));
System.out.println("now.plusDays(10) = " + now.plusDays(10));
System.out.println("now.plusHours(5) = " + now.plusHours(5));
System.out.println("now.plusMinutes(10) = " + now.plusMinutes(10));
System.out.println("now.plusSeconds(20) = " + now.plusSeconds(20));

// MINUS
System.out.println("now.minusYears(2) = " + now.minusYears(2));
System.out.println("now.minusMonths(5) = " + now.minusMonths(5));
System.out.println("now.minusDays(10) = " + now.minusDays(10));
System.out.println("now.minusHours(5) = " + now.minusHours(5));
System.out.println("now.minusMinutes(10) = " + now.minusMinutes(10));
System.out.println("now.minusSeconds(20) = " + now.minusSeconds(20));

System.out.println("now = " + now);
output:

now = 2022-05-16T22:08:59.398040

now.plusYears(2) = 2024-05-16T22:08:59.398040
now.plusMonths(5) = 2022-10-16T22:08:59.398040
now.plusDays(10) = 2022-05-26T22:08:59.398040
now.plusHours(5) = 2022-05-17T03:08:59.398040
now.plusMinutes(10) = 2022-05-16T22:18:59.398040
now.plusSeconds(20) = 2022-05-16T22:09:19.398040

now.minusYears(2) = 2020-05-16T22:08:59.398040
now.minusMonths(5) = 2021-12-16T22:08:59.398040
now.minusDays(10) = 2022-05-06T22:08:59.398040
now.minusHours(5) = 2022-05-16T17:08:59.398040
now.minusMinutes(10) = 2022-05-16T21:58:59.398040
now.minusSeconds(20) = 2022-05-16T22:08:39.398040

now = 2022-05-16T22:08:59.398040

 

  • 가장 첫줄과 마지막 줄의 now 가 같은 것이 보일 것이다. plus, minus 메소드를 통해서 연산을 하더라도 객체 생성시 가지고 있는 값은 변하지 않는다.
  • plus, minus 메소드에서 다음 문구 확인 가능 : This instance is immutable and unaffected by this method call.

3.  비교(isBefore, isAfter, isEqual)

LocalDateTime now = LocalDateTime.now();
System.out.println("now = " + now);
LocalDateTime after2Years = now.plusYears(2);
System.out.println("after2Years = " + after2Years);

System.out.println("now.isBefore(after2Years) = " + now.isBefore(after2Years));
System.out.println("now.isAfter(after2Years) = " + now.isAfter(after2Years));
System.out.println("now.isEqual(after2Years) = " + now.isEqual(after2Years));
output:

now = 2022-05-16T22:18:59.727866

after2Years = 2024-05-16T22:18:59.727866

now.isBefore(after2Years) = true
now.isAfter(after2Years) = false
now.isEqual(after2Years) = false

4. Period

LocalDate startDate = LocalDate.now();
System.out.println("startDate = " + startDate);

LocalDate endDate = startDate.plusYears(2).plusMonths(3).plusDays(4);
System.out.println("endDate = " + endDate);

Period between = Period.between(startDate, endDate);

System.out.println("between.getYears() = " + between.getYears());
System.out.println("between.getMonths() = " + between.getMonths());
System.out.println("between.getDays() = " + between.getDays());
output:

startDate = 2022-05-17
endDate = 2024-08-21

between.getYears() = 2
between.getMonths() = 3
between.getDays() = 4
  • 날짜 또는 기간 차이에 대한 계산
  • 출력된 내용을 보면 getYear, getMonths, getDays 는 두 날짜의 시작과 끝의 대한 차이가 아니라는 것이 확인된다.
  • 각 항목에 대한 차이만 확인 가능하다.
    • 2022년과 2024년 차이 = 2년
    • 5월과 8월 차이 = 3
    • 17일과 4일 차이 = 4 
  • endDate가 startDate 보다 이전인 경우 음수로 표현된다. isNegative 메소드로 확인 가능

5. Duration

LocalDateTime startDate = LocalDateTime.now();
LocalDateTime endDate = startDate.plusMonths(4);
Duration between = Duration.between(startDate, endDate);
System.out.println("between = " + between);
System.out.println("between.toDays() = " + between.toDays());
System.out.println("between.toHours() = " + between.toHours());
System.out.println("between.toMinutes() = " + between.toMinutes());
System.out.println("between.toSeconds() = " + between.toSeconds());
output:

between = PT2952H
between.toDays() = 123
between.toHours() = 2952
between.toMinutes() = 177120
between.toSeconds() = 10627200
  • 시간 차이에 대한 계산
  • endDate가 startDate 보다 이전인 경우 음수로 표현된다. isNegative 메소드로 확인 가능

6. Date Format 지정하기

API 응답에 읽기 쉬운 포맷으로 날짜와 시간을 보내줘야 하는 경우가 있는데, DateTimeFormatter 를 사용하면 된다.

// 현재 날짜 및 시간
LocalDateTime now = LocalDateTime.now();

// ISO : yyyy-MM-ddTHH:mm:ss
String isoDateTime = now.format(DateTimeFormatter.ISO_DATE_TIME);
System.out.println("isoDateTime = " + isoDateTime);

// 사용자 입력
String customFormat1 = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println("customFormat1 = " + customFormat1);

// 오전 / 오후
String customFormat2 = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd a HH:mm:ss"));
System.out.println("customFormat2 = " + customFormat2);

// Date / Time 각각
String formatDate = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
System.out.println("formatDate = " + formatDate);
String formatTime = LocalTime.now().format(DateTimeFormatter.ofPattern("a HH:mm:ss"));
System.out.println("formatTime = " + formatTime);
output :

isoDateTime = 2022-05-17T00:44:24.137939

customFormat1 = 2022-05-17 00:44:24
customFormat2 = 2022-05-17 오전 00:44:24

formatDate = 2022-05-17
formatTime = 오전 00:44:24

 

'개발 > [Java] 사용하기' 카테고리의 다른 글

[LocalDateTime] 만들기  (0) 2022.05.16
[LocalDateTime] 기록  (0) 2022.05.16

+ Recent posts