[김영한의 실전 자바 - 중급 1편] 06. 날짜와 시간

Turtle·2024년 7월 8일
0
post-thumbnail

🙄자바 날짜와 시간 라이브러리 소개

🙄기본 날짜와 시간 - LocalDateTime

가장 기본이 되는 날짜와 시간 클래스는 LocalDate, LocalTime, LocalDateTime이다.

  • LocalDate : 날짜만 표현할 때 사용한다. Ex) 2013-11-21
  • LocalTime : 시간만을 표현할 때 사용한다. Ex) 08:20:30.213
  • LocalDateTime : LocalDate + LocalTime

주의 : 모든 날짜 클래스는 불변으로 설계되어있다. 따라서 변경이 발생하는 경우 새로운 객체를 생성해서 반환해야한다.

  • ✔️isEquals() vs equals()
    • isEquals()는 단순히 비교 대상이 시간적으로 같으면 true를 반환한다. 객체가 다르고 타임존이 달라도 시간적으로 같으면 true를 반환한다.
      • Ex. 서울의 9시와 UTC의 0시는 시간적으로 같다.
    • equals()는 객체의 타입, 타임존 등등 내부 데이터의 모든 구성요소가 같아야 true를 반환한다.
      • Ex. 서울의 9시와 UTC 0시는 시간적으로 같다. 이 둘을 비교하면 타임존의 데이터가 다르기 때문에 false가 된다.

🙄타임존 - ZonedDateTime

ZonedDateTimeLocalDateTime에 시간대 정보인 ZoneId가 합쳐진 것이다.

public class ZonedDateTimeMain {
	public static void main(String[] args) {
		ZonedDateTime nowZdt = ZonedDateTime.now();
		System.out.println("nowZdt = " + nowZdt);

		LocalDateTime ldt = LocalDateTime.of(2030, 1, 1, 13, 30, 50);
		ZonedDateTime zdt1 = ZonedDateTime.of(ldt, ZoneId.of("Asia/Seoul"));
		System.out.println("zdt1 = " + zdt1);

		ZonedDateTime zdt2 = ZonedDateTime.of(2030, 1, 1, 13, 30, 50, 0, ZoneId.of("Asia/Seoul"));
		System.out.println("zdt2 = " + zdt2);

		// withZoneSameInstant : 타임존 변경
		ZonedDateTime utcZdt = zdt2.withZoneSameInstant(ZoneId.of("UTC"));
		System.out.println("utcZdt = " + utcZdt);
	}
}

OffsetDateTimeLocalDateTime에 UTC 오프셋 정보인 ZoneOffset이 합쳐진 것이다.

public class OffsetDateTimeMain {
	public static void main(String[] args) {
		OffsetDateTime nowOdt = OffsetDateTime.now();
		System.out.println("nowOdt = " + nowOdt);

		LocalDateTime ldt = LocalDateTime.of(2030, 1, 1, 13, 30, 50);
		System.out.println("ldt = " + ldt);
		OffsetDateTime odt = OffsetDateTime.of(ldt, ZoneOffset.of("+01:00"));
		System.out.println("odt = " + odt);
	}
}

🙄기계 중심의 시간 - Instant

Instant는 UTC를 기준으로 하는 시간의 한 지점을 나타낸다. Instant 내부에는 초 데이터만 들어있다.(나노초 포함) 날짜와 시간을 계산하기에 부적합하다.

🙄기간, 시간의 간격 - Duration, Period

시간의 개념은 크게 2가지로 표현할 수 있다. (Period > Duration)

  • 특정 시점의 시간(시각)
  • 시간의 간격(기간)
  • ✔️Period
    • 두 날짜 사이의 간격을 년, 월, 일 단위로 나타낸다.
  • ✔️Duration
    • 두 시간 사이의 간격을 시, 분, 초 단위로 나타낸다.

public class PeriodMain {
	public static void main(String[] args) {
		Period period = Period.ofDays(10);
		System.out.println("period = " + period);

		LocalDate currentDate = LocalDate.of(2030, 1, 1);
		LocalDate plusDate = currentDate.plus(period);
		System.out.println("currentDate = " + currentDate);
		System.out.println("plusDate = " + plusDate);

		LocalDate startDate = LocalDate.of(2023, 1, 1);
		LocalDate endDate = LocalDate.of(2023, 4, 2);
		Period between = Period.between(startDate, endDate);
		System.out.println("기간 : " + between.getMonths() + "개월 " + between.getDays() + "일");
	}
}
public class DurationMain {
	public static void main(String[] args) {
		Duration duration = Duration.ofMinutes(30);
		System.out.println("duration = " + duration);

		LocalTime localTime = LocalTime.of(1, 0);
		System.out.println("localTime = " + localTime);

		LocalTime plusTime = localTime.plus(duration);
		System.out.println("plusTime = " + plusTime);

		LocalTime start = LocalTime.of(9, 0);
		LocalTime end = LocalTime.of(18, 0);
		Duration between = Duration.between(start, end);
		System.out.println("차이 : " + between.getSeconds() + "초");
		System.out.println("근무 시간 : " + between.toHours() + "시간 " + between.toMinutesPart() + "분");
	}
}

🙄날짜와 시간의 핵심 인터페이스

  • 특정 시점의 시간 : Temporal(TemporalAccessor 포함) 인터페이스를 구현한다.
    • 구현으로 LocalDateTime, LocalDate, LocalTime, ZonedDateTime, OffsetDateTime, Instant 등이 있다.
  • 시간의 간격(기간) : TemporalAmount 인터페이스를 구현한다.
    • 구현으로 Duration, Period 등이 있다.
  • ✔️TemporalAccessor 인터페이스
    • 날짜와 시간을 읽기 위한 기본 인터페이스
    • 이 인터페이스는 특정 시점의 날짜와 시간 정보를 읽을 수 있는 최소한의 기능을 제공한다.
  • ✔️Temporal 인터페이스
    • TemporalAccessor의 하위 인터페이스로, 날짜와 시간을 조작하기 위한 기능을 제공한다.
    • 이를 통해 날짜와 시간을 변경하거나 조정할 수 있다.
  • ✔️TemporalAmount 인터페이스
    • 시간의 간격을 나타내며, 날짜와 시간 객체에 적용하여 그 객체를 조정할 수 있다. 특정 날짜에 일정 기간을 더하거나 빼는 곳에 사용된다.

🙄날짜와 시간 조회하고 조작하기

public class GetTimeMain {
	public static void main(String[] args) {
		LocalDateTime dt = LocalDateTime.of(2030, 1, 1, 13, 30, 59);
		System.out.println("YEAR : " + dt.get(ChronoField.YEAR));
		System.out.println("MONTH_OF_YEAR : " + dt.get(ChronoField.MONTH_OF_YEAR));
		System.out.println("DAY_OF_MONTH : " + dt.get(ChronoField.DAY_OF_MONTH));
		System.out.println("HOUR_OF_DAY : " + dt.get(ChronoField.HOUR_OF_DAY));
		System.out.println("MINUTE_OF_HOUR : " + dt.get(ChronoField.MINUTE_OF_HOUR));
		System.out.println("SECOND_OF_MINUTE : " + dt.get(ChronoField.SECOND_OF_MINUTE));

		System.out.println("편의 메서드 제공");
		System.out.println("YEAR : " + dt.getYear());
		System.out.println("MONTH_OF_YEAR : " + dt.getMonthValue());
		System.out.println("DAY_OF_MONTH : " + dt.getDayOfMonth());
		System.out.println("HOUR_OF_DAY : " + dt.getHour());
		System.out.println("MINUTE_OF_HOUR : " + dt.getMinute());
		System.out.println("SECOND_OF_MINUTE : " + dt.getSecond());

		System.out.println("편의 메서드 없음");
		System.out.println("MINUTE_OF_DAY = " + dt.get(ChronoField.MINUTE_OF_DAY));
		System.out.println("SECOND_OF_DAY = " + dt.get(ChronoField.SECOND_OF_DAY));
	}
}
public class ChangeTimePlusMain {
	public static void main(String[] args) {
		LocalDateTime dt = LocalDateTime.of(2018, 1, 1, 13, 30, 59);
		System.out.println("dt = " + dt);

		LocalDateTime plusDt1 = dt.plus(10, ChronoUnit.YEARS);
		System.out.println("plusDt1 = " + plusDt1);

		LocalDateTime plusDt2 = dt.plusYears(10);
		System.out.println("plusDt2 = " + plusDt2);

		Period period = Period.ofYears(10);
		LocalDateTime plusDt3 = dt.plus(period);
		System.out.println("plusDt3 = " + plusDt3);
	}
}
public class ChangeTimeWithMain {
	public static void main(String[] args) {
		LocalDateTime dt = LocalDateTime.of(2018, 1, 1, 13, 30, 59);
		System.out.println("dt = " + dt);

		LocalDateTime changeDt1 = dt.with(ChronoField.YEAR, 2020);
		System.out.println("changeDt1 = " + changeDt1);

		LocalDateTime changeDt2 = dt.withYear(2020);
		System.out.println("changeDt2 = " + changeDt2);

		LocalDateTime with1 = dt.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));
		System.out.println("기준 날짜 : " + dt);
		System.out.println("다음주 금요일 : " + with1);

		LocalDateTime with2 = dt.with(TemporalAdjusters.lastInMonth(DayOfWeek.SUNDAY));
		System.out.println("같은 달의 마지막 일요일 : " + with2);
	}
}

🙄날짜와 시간 문자열 파싱과 포맷팅

public class FormattingMain {
	public static void main(String[] args) {
		LocalDate localDateTime = LocalDate.of(2024, 12, 31);
		System.out.println("localDateTime = " + localDateTime);

		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy년 MM월 dd일");
		String format = localDateTime.format(formatter);
		System.out.println("format = " + format);

		String input = "2030년 01월 01일";
		LocalDate parseDate = LocalDate.parse(input, formatter);
		System.out.println("parseDate = " + parseDate);
	}
}

0개의 댓글