본문 바로가기

언어/자바

java.util.Date와 LocalDateTime

java에서는 jdk 1.0부터 지원해오는 시간관련 클래스가 있다.

바로 Date 클래스이다.

오늘날 jdk 버전이 12까지 올라왔고 이 Date 클래스는 여전히 많이 사용되고 있다.

하지만 이 Date 클래스에는 여러 문제점이 있다.

 

1. 가변적이다. (== not immutable)

 

2. 포맷이 없다.

 

3. calendar system이 없다.

 

4. 월이 1~12가 아니라 0~11이다. (헷갈릴 수 있음)

 

이런 문제점을 안고 있는 Date 를 보완하기 위해 Calendar 라는 클래스가 추가됐다.

하지만 이 마저 시원치않아 joda와 같은 라이브러리를 사용하기도 했다고 한다. (사용해본적 없음)

 

jdk 8에 이르러서야 비로소 새로운 시간, 날짜 관리 클래스가 선보여졌다.

바로 java.time 패키지 하위의 클래스 들이다.

많은 클래스들이 있지만 대표적으로 LocalTime, LocalDate, LocalDateTime이 있다.

 

LocalTime:

A time without a time-zone in the ISO-8601 calendar system, such as 10:15:30.

LocalTime is an immutable date-time object that represents a time, often viewed as hour-minute-second. Time is represented to nanosecond precision. For example, the value "13:45.30.123456789" can be stored in a LocalTime.

 

LocalDate:

A date without a time-zone in the ISO-8601 calendar system, such as 2007-12-03.

LocalDate is an immutable date-time object that represents a date, often viewed as year-month-day. Other date fields, such as day-of-year, day-of-week and week-of-year, can also be accessed. For example, the value "2nd October 2007" can be stored in a LocalDate.

 

LocalDateTime: 

A date-time without a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30.

LocalDateTime is an immutable date-time object that represents a date-time, often viewed as year-month-day-hour-minute-second. Other date and time fields, such as day-of-year, day-of-week and week-of-year, can also be accessed. Time is represented to nanosecond precision. For example, the value "2nd October 2007 at 13:45.30.123456789" can be stored in a LocalDateTime.

 

참고: 

https://d2.naver.com/helloworld/645609

https://github.com/benelog/java-date-time/blob/master/jdk8-date-time/src/test/java/com/nbp/weblab/date/Jsr310Test.java