4.9 KiB
4.9 KiB
자바 Date-Time API 쉽게 배우기
1. Date-Time API란?
자바에서 날짜와 시간을 다루는 API는 크게 두 가지가 있다.
java.util.Date,java.util.Calendar(구버전, 사용 비추천)java.time패키지 (Java 8 이후, 최신 API)
Java 8부터 java.time 패키지가 추가되면서 날짜와 시간을 더 직관적이고 강력하게 다룰 수 있게 되었다.
이번 글에서는 java.time 패키지를 중심으로 설명하겠다.
2. 주요 클래스 및 메서드 정리
(1) LocalDate (날짜만 다루는 클래스)
| 메서드 | 설명 |
|---|---|
now() |
현재 날짜 가져오기 |
of(year, month, dayOfMonth) |
특정 날짜 생성 |
plusDays(n), minusDays(n) |
n일 더하기 / 빼기 |
getYear(), getMonth(), getDayOfMonth() |
연도, 월, 일 가져오기 |
isBefore(date), isAfter(date) |
날짜 비교 |
예제 코드
LocalDate today = LocalDate.now();
System.out.println(today); // 2025-03-06
LocalDate specificDate = LocalDate.of(2025, 12, 25);
System.out.println(specificDate); // 2025-12-25
LocalDate nextWeek = today.plusDays(7);
System.out.println(nextWeek); // 2025-03-13
(2) LocalTime (시간만 다루는 클래스)
| 메서드 | 설명 |
|---|---|
now() |
현재 시간 가져오기 |
of(hour, minute, second) |
특정 시간 생성 |
plusHours(n), minusMinutes(n) |
시간 또는 분 더하기 / 빼기 |
getHour(), getMinute(), getSecond() |
시, 분, 초 가져오기 |
예제 코드
LocalTime now = LocalTime.now();
System.out.println(now); // 14:30:15 (예시)
LocalTime meetingTime = LocalTime.of(10, 30);
System.out.println(meetingTime); // 10:30
LocalTime afterOneHour = now.plusHours(1);
System.out.println(afterOneHour); // 15:30:15 (예시)
(3) LocalDateTime (날짜 + 시간)
| 메서드 | 설명 |
|---|---|
now() |
현재 날짜와 시간 가져오기 |
of(LocalDate, LocalTime) |
특정 날짜와 시간 생성 |
plusDays(n), plusHours(n) |
날짜/시간 더하기 |
getYear(), getMonth(), getDayOfMonth() |
날짜 정보 가져오기 |
예제 코드
LocalDateTime now = LocalDateTime.now();
System.out.println(now); // 2025-03-06T14:30:15 (예시)
LocalDateTime eventTime = LocalDateTime.of(2025, 12, 25, 18, 30);
System.out.println(eventTime); // 2025-12-25T18:30
(4) ZonedDateTime (시간대 정보 포함)
| 메서드 | 설명 |
|---|---|
now(ZoneId.of("Asia/Seoul")) |
특정 시간대의 현재 시간 가져오기 |
of(year, month, day, hour, min, sec, ZoneId.of("UTC")) |
특정 시간대의 날짜/시간 생성 |
예제 코드
ZonedDateTime seoulTime = ZonedDateTime.now(ZoneId.of("Asia/Seoul"));
System.out.println(seoulTime); // 2025-03-06T14:30:15+09:00[Asia/Seoul]
(5) 날짜 포맷 변환 (DateTimeFormatter)
| 메서드 | 설명 |
|---|---|
ofPattern("yyyy-MM-dd HH:mm") |
날짜/시간 형식 지정 |
format(DateTimeFormatter formatter) |
날짜/시간을 문자열로 변환 |
parse(String, DateTimeFormatter formatter) |
문자열을 날짜/시간으로 변환 |
예제 코드
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
String formattedDate = now.format(formatter);
System.out.println(formattedDate); // 2025-03-06 14:30
(6) 날짜 차이 계산 (Duration, Period)
| 클래스 | 설명 |
|---|---|
Duration.between(time1, time2) |
시간 차이 계산 (초, 분, 시간 단위) |
Period.between(date1, date2) |
날짜 차이 계산 (년, 월, 일 단위) |
예제 코드
LocalDate date1 = LocalDate.of(2025, 1, 1);
LocalDate date2 = LocalDate.of(2025, 3, 6);
Period period = Period.between(date1, date2);
System.out.println(period.getMonths() + "개월 " + period.getDays() + "일"); // 2개월 5일
LocalTime time1 = LocalTime.of(10, 0);
LocalTime time2 = LocalTime.of(12, 30);
Duration duration = Duration.between(time1, time2);
System.out.println(duration.toHours() + "시간 " + duration.toMinutes() + "분"); // 2시간 30분
3. 정리
✅ LocalDate : 날짜만 다룸 (2025-03-06)
✅ LocalTime : 시간만 다룸 (14:30:15)
✅ LocalDateTime : 날짜 + 시간 (2025-03-06T14:30:15)
✅ ZonedDateTime : 시간대 정보 포함 (+09:00[Asia/Seoul])
✅ DateTimeFormatter : 날짜 형식 변환 (yyyy-MM-dd HH:mm)
✅ Period / Duration : 날짜/시간 차이 계산
자바의 java.time 패키지를 활용하면 날짜와 시간을 더욱 쉽게 관리할 수 있다!