✅ java.time.LocalDate 는 날짜(년, 월, 일) 만 다루는 클래스
java.time 패키지 (JSR-310) 에 속해 있음.Date , Calendar 의 단점을 보완하려고 만들어졌음LocalDate 객체는 바뀌지 않는다.LocalDate.now() 는 시스템 기본 시간대의 현재 날짜를 가져옴.ZoneId 지정하면 다른 지역 날짜도 뽑을 수 있다.)Period 클래스 활용)import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.Period;
public class LocalDateExample {
public static void main(String[] args) {
// 📌 현재 날짜
LocalDate today = LocalDate.now();
System.out.println("오늘 날짜: " + today);
// 📌 특정 날짜 생성
LocalDate birthday = LocalDate.of(1995, 5, 23);
System.out.println("생일: " + birthday);
// 📌 문자열 파싱
LocalDate parsed = LocalDate.parse("2025-09-25");
System.out.println("파싱된 날짜: " + parsed);
// 📌 날짜 연산
System.out.println("내일: " + today.plusDays(1));
System.out.println("지난주: " + today.minusWeeks(1));
// 📌 날짜 비교
System.out.println("생일이 오늘 이전인가? " + birthday.isBefore(today));
System.out.println("생일이 오늘 이후인가? " + birthday.isAfter(today));
// 📌 날짜 차이
Period period = Period.between(birthday, today);
System.out.println("살아온 년/월/일: " + period.getYears() + "년 "
+ period.getMonths() + "개월 " + period.getDays() + "일");
// 📌 포맷팅
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy년 MM월 dd일");
System.out.println("포맷팅된 날짜: " + today.format(formatter));
}
}
now() : 현재 날짜
of(int year, int month, int day) : 특정 날짜 만들기
parse(String text) : 문자열을 날짜로 변환
plusDays() / plusMonths() / plusYears() : 날짜 더하기
minusDays() / minusWeeks() / minusYears() : 날짜 빼기
isBefore() / isAfter() / isEqual() : 날짜 비교
getYear() , getMonth() , getDayOfMonth() : 년/월/일 가져오기
format(DateTimeFormatter) : 원하는 문자열 형태로 변환