Java Date - 날짜 코딩 연습

주어진 문제를 보고 풀어보기
단, time.test 패키지를 사용

연습 문제 - 날짜 더하기

설명

  • 2025년 1월 1일 0시 0분 0초로 객체로 생성하고 1년 2개월 3일 4시간 후의 시각을 표현하기

풀이

LocalDateTime dt = LocalDateTime.of(2025, 1, 1, 0, 0, 0);
Period period = Period.of(1, 2, 3);
Duration duration = Duration.of(4, ChronoUnit.HOURS);
LocalDateTime out = dt.plus(period).plus(duration);

System.out.println(out);

2026-03-04T04:00


연습 문제 - 날짜 간격 반복 출력해보기

설명

  • 2025년 1월 1일 부터 2주 간격으로 4번 반복하여 날짜 출력해보기

풀이

LocalDate dt = LocalDate.of(2025, 1, 1);
for (int i = 0; i < 4; i++) {
    System.out.print("횟수" + (i+1) + ": " );
    System.out.println(dt.plus((i)*2, ChronoUnit.WEEKS));
}

2025-01-01
2025-01-15
2025-01-29
2025-02-12


연습 문제 - 디데이 구해보기

설명

  • 주어진 시작 날짜와 목표 날짜를 이용해 남은 기간의 디데이를 구하라
  • 남은 기간은 "y년 M개월 d일" 형식으로 출력한다.
  • 디데이는 "d일" 포맷으로 출력한다.

시작날짜와 목표날짜

LocalDate startDate = LocalDate.of(2025, 1, 1);
LocalDate endDate = LocalDate.of(2025,9,21);

풀이

LocalDate startDate = LocalDate.of(2025, 1, 1);
LocalDate endDate = LocalDate.of(2025,9,21);
Period p = Period.between(startDate, endDate);

System.out.println("프로젝트 시작 날짜: " + startDate);
System.out.println("프로젝트 종료 날짜: " + endDate);
System.out.println("프로젝트 남은 기간: " + p.getYears() + "년 " + p.getMonths() + "개월 " + p.getDays() + "일");
System.out.println("디데이: " + ChronoUnit.DAYS.between(startDate, endDate) + "일");

프로젝트 시작 날짜: 2025-01-01
프로젝트 종료 날짜: 2025-09-21
프로젝트 남은 기간: 0년 8개월 20일
디데이: 263일


연습 문제 - 시작 요일, 마지막 요일 구하기

설명

  • 주어진 2025년과 9월로 첫날 요일과 마지막날의 요일을 구하라.

예상 결과

firstDayOfWeek = MONDAY
lastDayOfWeek = WEDNESDAY

풀이

int year = 2025;
int month = 9;
LocalDate dt = LocalDate.of(year, month, 1);
DayOfWeek firstDayOfWeek = dt.getDayOfWeek();
DayOfWeek lastDayOfWeek = dt.with(TemporalAdjusters.lastDayOfMonth()).getDayOfWeek();

System.out.println("firstDayOfWeek: " + firstDayOfWeek);
System.out.println("lastDayOfWeek: " + lastDayOfWeek);

firstDayOfWeek: MONDAY
lastDayOfWeek: TUESDAY

DayOfWeek toString 메소드가 문자열로 날짜로 반환한다.


연습 문제 - 국제 회의 시간

  • 서울의 회의 시간은 2025년 1월 1일 오전 9시
  • 런던과 뉴욕의 시간 구하기

예상 결과

서울 회의 시간: 2025-01-01T09:00+09:00[Asia/Seoul]
뉴욕 회의 시간: 2024-12-31T19:00-05:00[America/New_York]
런던 회의 시간: 2025-01-01T00:00Z[Europe/London]

풀이

LocalDate date = LocalDate.of(2025, 1, 1);
LocalTime time = LocalTime.of(9, 0);
ZonedDateTime seoulTime = ZonedDateTime.of(date, time, ZoneId.of("Asia/Seoul"));
ZonedDateTime londonTime = seoulTime.withZoneSameInstant(ZoneId.of("Europe/London"));
ZonedDateTime newYorkTime = seoulTime.withZoneSameInstant(ZoneId.of("America/New_York"));

System.out.println("서울 회의 시간: " + seoulTime);
System.out.println("뉴욕 회의 시간: " + newYorkTime);
System.out.println("런던 회의 시간: " + londonTime);

withZoneSameInstant 메소드 외의 withZoneSameLocal 메소드도 있다.
어떤 메소드인지 꼭 확인하고 사용한다.

ZonedDateTime newYorkTime = seoulTime.withZoneSameInstant(ZoneId.of("America/New_York"));
ZonedDateTime newYorkTime2 = seoulTime.withZoneSameLocal(ZoneId.of("America/New_York"));


System.out.println("뉴욕 회의 시간 Instant: " + newYorkTime);
System.out.println("뉴욕 회의 시간 Local  : " + newYorkTime2);

뉴욕 회의 시간 Instant: 2024-12-31T19:00-05:00[America/New_York]
뉴욕 회의 시간 Local : 2025-01-01T09:00-05:00[America/New_York]