기본이 되는 날짜와 시간 클래스는 LocalDate
, LocalTime
, LocalDateTime
이다.
이 클래스들은 세계 시간대를 고려하지 않아 타임존이 적용되지 않는다. 그래서 주로 특정 지역의 날짜와 시간만 고려하면 되는 경우에 사용한다.
날짜를 표현할 때 사용하며, 년, 월, 일을 다룬다.
메서드 | 설명 |
---|---|
getYear() | 연도 반환 |
getMonthValue() | 월을 숫자로 반환. 예: 5 |
getMonth() | 월을 열거형으로 반환. 예: MAY |
getDayOfMonth() | 일 반환 |
getDayOfYear() | 해당 날짜가 해당 연도의 몇 번째 날인지 반환 |
getDayOfWeek() | 요일 반환. 예: MONDAY |
lengthOfMonth() | 해당 월의 일 수 반환 |
lengthOfYear() | 해당 연도의 일 수 반환 |
isLeapYear() | 해당 연도가 윤년인지 여부 반환 |
isBefore(ChronoLocalDate other) | 다른 날짜와 비교. 현재 날짜보다 이전이면 true 반환 |
isAfter(ChronoLocalDate other) | 다른 날짜와 비교. 현재 날짜보다 이후면 true 반환 |
isEqual(ChronoLocalDate other) | 다른 날짜와 비교. 현재 날짜와 같으면 true 반환 |
plusYears(long yearsToAdd) | 특정 연수를 더하여 새로운 LocalDate 객체를 반환 |
plusMonths(long monthsToAdd) | 특정 월수를 더하여 새로운 LocalDate 객체를 반환 |
plusWeeks(long weeksToAdd) | 특정 주수를 더하여 새로운 LocalDate 객체를 반환 |
plusDays(long daysToAdd) | 특정 일수를 더하여 새로운 LocalDate 객체를 반환 |
minusYears(long yearsToSubtract) | 특정 연수를 빼서 새로운 LocalDate 객체를 반환 |
minusMonths(long monthsToSubtract) | 특정 월수를 빼서 새로운 LocalDate 객체를 반환 |
minusWeeks(long weeksToSubtract) | 특정 주수를 빼서 새로운 LocalDate 객체를 반환 |
minusDays(long daysToSubtract) | 특정 일수를 빼서 새로운 LocalDate 객체를 반환 |
withYear(int year) | 연도를 변경하여 새로운 LocalDate 객체를 반환 |
withMonth(int month) | 월을 변경하여 새로운 LocalDate 객체를 반환 withDayOfMonth(int dayOfMonth) |
public class LocalDateMain {
public static void main(String[] args) {
// 1. 날짜 생성
LocalDate nowDate = LocalDate.now();
LocalDate ofDate = LocalDate.of(2024, 05, 31);
System.out.println("오늘 날짜: " + nowDate);
System.out.println("지정 날짜: " + ofDate);
// 2. 날짜 조회
System.out.println("월(열거형): " + nowDate.getMonth());
System.out.println("월(숫자): " + nowDate.getMonthValue()); // getMonth().getValue()
System.out.println("5월의 일 수: " + ofDate.lengthOfMonth());
// 3. 날짜 계산(불변 객체)
LocalDate plusDays = ofDate.plusDays(10);
LocalDate minusDays = ofDate.minusDays(7);
System.out.println("지정 날짜 + 10 = " + plusDays);
System.out.println("지정 날짜 - 7 = " + minusDays);
}
}
오늘 날짜: 2024-06-03
지정 날짜: 2024-05-31
월(열거형): MAY
월(숫자): 5
5월의 일 수: 31
지정 날짜 + 10 = 2024-06-10
지정 날짜 - 7 = 2024-05-24
시간을 표현할 때 사용하며, 시, 분, 초를 다룬다.
메서드 | 설명 |
---|---|
getHour() | 시 반환 |
getMinute() | 분 반환 |
getSecond() | 초 반환 |
getNano() | 나노초 반환 |
isBefore(LocalTime other) | 다른 시간과 비교. 현재 시간보다 이전이면 true 반환 |
isAfter(LocalTime other) | 다른 시간과 비교. 현재 시간보다 이후면 true 반환 |
plusHours(long hoursToAdd) | 특정 시간만큼 더하여 새로운 LocalTime 객체 반환 |
minusHours(long hoursToSubtract) | 특정 시간만큼 빼서 새로운 LocalTime 객체 반환 |
withHour(int hour) | 시를 변경하여 새로운 LocalTime 객체 반환 |
withMinute(int minute) | 분을 변경하여 새로운 LocalTime 객체 반환 |
withSecond(int second) | 초를 변경하여 새로운 LocalTime 객체 반환 |
public class LocalTimeMain {
public static void main(String[] args) {
// 1. 시간 생성
LocalTime nowTime = LocalTime.now();
LocalTime ofTime = LocalTime.of(20,50,10);
System.out.println("현재 시간: " + nowTime);
System.out.println("지정 시간: " + ofTime);
// 2. 시간 조회
System.out.println(nowTime.getHour() + "시");
System.out.println(nowTime.getMinute() + "분");
// 3. 시간 계산(불변 객체)
LocalTime ofTimePlus = ofTime.plusSeconds(10);
System.out.println("10초 후: " + ofTimePlus);
}
}
현재 시간: 18:39:31.089726
지정 시간: 20:50:10
18시
39분
10초 후: 20:50:20
LocalDate
와 LocalTime
을 내부에 가지고 날짜와 시간을 모두 표현한다.
public final class LocalDateTime
implements Temporal, TemporalAdjuster, ChronoLocalDateTime<LocalDate>, Serializable {
...
private final LocalDate date;
private final LocalTime time;
...
}
메서드 | 설명 |
---|---|
getYear() | 연도 반환 |
getMonthValue() | 월을 숫자로 반환. 예: 5 |
getMonth() | 월을 열거형으로 반환. 예: MAY |
getDayOfMonth() | 일 반환 |
getDayOfYear() | 해당 날짜가 해당 연도의 몇 번째 날인지 반환 |
getDayOfWeek() | 요일 반환. 예: MONDAY |
lengthOfMonth() | 해당 월의 일 수 반환 |
lengthOfYear() | 해당 연도의 일 수 반환 |
getHour() | 시 반환 |
getMinute() | 분 반환 |
getSecond() | 초 반환 |
getNano() | 나노초 반환 |
isLeapYear() | 해당 연도가 윤년인지 여부 반환 |
isAfter(ChronoLocalDateTime<?> other) | 현재 날짜, 시간과 비교. 이후면 true 반환 |
isBefore(ChronoLocalDateTime<?> other) | 현재 날짜, 시간과 비교. 이전이면 true 반환 |
isEqual(ChronoLocalDateTime<?> other) | 현재 날짜, 시간과 비교. 같으면 true 반환 |
plusYears(long years) | 특정 연수를 더하여 새로운 LocalDateTime 객체를 반환 |
plusHours(long hours) | 특정 시간만큼 더하여 새로운 LocalDateTime 객체 반환 |
minusYears(long years) | 특정 연수를 빼서 새로운 LocalDateTime 객체를 반환 |
minusHours(long hours) | 특정 시간만큼 빼서 새로운 LocalDateTime 객체 반환 |
withYear(int year) | 연도 변경하여 새로운 LocalDateTime 객체 반환 |
withHour(int hour) | 시를 변경하여 새로운 LocalDateTime 객체 반환 |
public class LocalDateTimeMain {
public static void main(String[] args) {
// 1. 날짜와 시간 생성
// 1-1. LocalDate + LocalTime
LocalDate nowDate = LocalDate.now();
LocalTime nowTime = LocalTime.now();
LocalDateTime dt1 = LocalDateTime.of(nowDate, nowTime);
LocalDateTime dt2 = nowDate.atTime(nowTime);
LocalDateTime dt3 = nowTime.atDate(nowDate);
System.out.println("dt1: " + dt1);
System.out.println("dt2: " + dt2);
System.out.println("dt3: " + dt3);
// 1-2. LocalDateTime
LocalDateTime nowDt = LocalDateTime.now();
LocalDateTime ofDt = LocalDateTime.of(2024, 5, 31, 14, 30 , 10);
System.out.println("현재 날짜와 시간: " + nowDt);
System.out.println("지정 날짜와 시간: " + ofDt);
// 2. 날짜와 시간 분리
LocalDate localDate = ofDt.toLocalDate();
LocalTime localTime = ofDt.toLocalTime();
System.out.println("localDate: " + localDate);
System.out.println("localTime: " + localTime);
// 3. 날짜와 시간 계산
LocalDateTime ofDtPlus3Months = ofDt.plusMonths(3);
LocalDateTime ofDtMinus2Hours = ofDt.minusHours(2);
System.out.println("3개월 후: " + ofDtPlus3Months);
System.out.println("2시간 전: " + ofDtMinus2Hours);
// 4. 날짜와 시간 비교
System.out.println(nowDt.isBefore(ofDt));
System.out.println(nowDt.isAfter(ofDt));
System.out.println(nowDt.isEqual(ofDt));
}
}
dt1: 2024-06-03T16:03:57.919748
dt2: 2024-06-03T16:03:57.919748
dt3: 2024-06-03T16:03:57.919748
현재 날짜와 시간: 2024-06-03T16:03:57.920499
지정 날짜와 시간: 2024-05-31T14:30:10
localDate: 2024-05-31
localTime: 14:30:10
3개월 후: 2024-08-31T14:30:10
2시간 전: 2024-05-31T12:30:10
false
true
false
참고
Class LocalDate
Class LocalTime
LocalDateTime
Java의 정석
김영한의 실전 자바 - 중급 1편