Post

[Java] 날짜/시간 관련 클래스 정리

Java 날짜/시간 API의 변화


Java 8 이전에는 java.util 패키지의 Date, Calendar 를 사용했으나,
불변이 아니고, 사용이 어려웠기 때문에

Java 8 부터는 java.time 패키지의 클래스들을 사용하게 되었다.

  • 불변 객체
  • 명확한 시간 계산
  • 직관적인 사용법
  • 다양한 기능 제공 (타임존 등)

Java 8 이상에서는 무조건 java.time 패키지 사용 권장

주요 클래스


카테고리클래스설명
날짜LocalDate날짜(년,월,일)
시간LocalTime시간(시,분,초,나노초)
날짜 + 시간LocalDateTime날짜와 시간
시간대 포함ZonedDateTime시간대까지 포함된 날짜시간
날짜 차이Period날짜 사이의 간격
시간 차이Duration시간 사이의 간격
포맷DateTimeFormatter문자열 ↔ 객체 변환
기타Year, YearMonth, MonthDay부분 날짜 표현

날짜 (LocalDate)


현재 날짜는 2025-08-01 12:00:00 을 기준으로 설명

1
2
3
4
5
6
7
8
9
10
11
12
13
LocalDate today = LocalDate.now(); // 현재 날짜: 2025-08-01
LocalDate birth = LocalDate.of(1995, 5, 23); // 지정 날짜: 1995-05-23
LocalDate Christmas = LocalDate.of(2025, 12, 25);

LocalDate tomorrow = today.plusDays(1); // +1일: 2025-08-02
LocalDate nextWeek = today.plusWeeks(1); // +7일: 2025-08-08
LocalDate tenYearsLater = today.plusYears(10); // +10년: 2035-08-01
LocalDate changed = birth.withYear(2020); // 날짜 변경: 2020-08-01
LocalDate prevMonth = today.minusMonths(2); // -2개월: 2025-06-01

LocalDate parsed = LocalDate.parse("2025-08-01") // 문자열 → 날짜

DayOfWeek dayOfWeek = Christmas.getDayOfWeek(); // 요일 확인(enum 객체 반환)

시간 (LocalTime)


1
2
3
4
5
6
7
8
9
10
11
12
LocalTime now = LocalTime.now();
LocalTime lunchTime = LocalTime.of(12, 30); // 12:30
LocalTime timeToSleep = LocalTime.of(22, 30, 15); // 22:30:15

LocalTime plus = lunchTime.plusMinutes(90); // 90분 후: 14:00

// 메서드 체이닝
LocalTime methodChaining = lunchTime.plusHours(1)
                                    .plusMinutes(30); // 14:00

boolean isBefore = lunchTime.isBefore(timeToSleep); // true
boolean isAfter = lunchTime.isAfter(timeToSleep); // false

날짜 + 시간 (LocalDateTime)


1
2
3
4
5
6
7
8
9
10
11
LocalDateTime now = LocalDateTime.now();
LocalDateTime future = now.plusDays(3).withHour(10);

LocalDateTime expirationDate = LocalDateTime.of(2028, 10, 28, 14, 20); // 2028-10-28T14:20
LocalDateTime methodChaining = expirationDate
    .minusYears(2)
    .plusMonths(1)
    .minusDays(1)
    .withSecond(10)
    .plusNanos(100); // 2026-11-27T14:20:10.000000100

시간대 포함 (ZonedDateTime)


1
2
3
4
5
6
7
8
9
10
11
12
13
ZonedDateTime seoul = ZonedDateTime.now(ZoneId.of("Asia/Seoul")); // 서울 기준

ZonedDateTime nowHere = ZonedDateTime.now();

// 현재 시간대
String hereZone = nowHere.getZone().toString(); // "Asia/Seoul"

// UTC 기준 시간 (서울은 +09:00)
ZonedDateTime seoulNewYear = ZonedDateTime.of(
        2025, 1, 1,
        0, 0, 0, 0,
        ZoneId.of("Asia/Seoul")
);

날짜 사이의 간격 (Period)


1
2
3
4
5
6
7
8
LocalDate birth = LocalDate.of(1997, 1, 3);
LocalDate today = LocalDate.now();
Period gap = Period.between(birth, today);

// 각각 표시
int gapYears = gap.getYears(); // 년
int gapMonths = gap.getMonths(); // 월
int gapDays = gap.getDays(); // 일

시간 사이의 간격 (Duration)


1
2
3
4
5
6
7
8
9
LocalTime lunchTime = LocalTime.of(12, 00);
LocalTime dinnerTime = LocalTime.of(18, 30);
Duration gap = Duration.between(lunchTime, dinnerTime);

// 환산 개념 (Period 랑 다름)
long gapDays = gap.toDays(); // 일: 0
long gapHours = gap.toHours(); // 시: 6
long gapMinutes = gap.toMinutes(); // 분: 390
long gapSeconds = gap.toSeconds(); // 초: 23400

포맷 (DateTimeFormatter)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
LocalDateTime today = LocalDateTime.now();

DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy.MM.dd");
String formatted1 = today.format(formatter1); // 2025.08.01

DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
String formatted2 = today.format(formatter2); // 2025/08/01 12:00:00

DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yy.MM.dd");
String formatted3 = today.format(formatter3); // 25.08.01

DateTimeFormatter formatter4 = DateTimeFormatter.ofPattern("dd/MM/yyyy hh a");
String formatted4 = today.format(formatter4); // 01/08/2025 12 오후

DateTimeFormatter formatter5 = DateTimeFormatter.ofPattern("yyyy년 MM월 dd일 HH시");
String formatted5 = today.format(formatter5); // 2025년 08월 01일 12시

기타


1
2
3
4
5
6
7
Year year = Year.now();
YearMonth ym = YearMonth.of(2025, 7);
MonthDay birthday = MonthDay.of(12, 25);

System.out.println("현재 연도: " + year);
System.out.println("카드 유효기간: " + ym);
System.out.println("생일: " + birthday);