들어가면서
이번 글에서는 Java에서 제공하는 시간과 관련된 Class를 정리한다.
자바 시간 객체 종류
Date
public class Date
implements java.io.Serializable, Cloneable, Comparable<Date>
{
...
private transient long fastTime;
...
}
The class {@code Date} represents a specific instant* in time, with millisecond precision.
java.util 패키지에 있는 클래스다.
좀 더 쉽게 풀어쓰면 1970년 1월 1일 00:00:00 GMT를 기준으로 지금 몇 milisecond이 지났는지를 저장한다. (이런 개념을 epoch-seconds 라고 부른다.)
@Test
void date_test() {
Date date = new Date();
System.out.println("date.getTime:" + date.getTime());
System.out.println("date.toString: " + date.toString());
}
//output
date.getTime:1709987709840 // 1970.01.01 일 기준 지난 millisecond
date.toString: Sat Mar 09 21:35:09 KST 2024
Instant
@jdk.internal.ValueBased
public final class Instant
implements Temporal, TemporalAdjuster, Comparable<Instant>, Serializable {
/**
* The number of seconds from the epoch of 1970-01-01T00:00:00Z.
*/
private final long seconds;
/**
* The number of nanoseconds, later along the time-line, from the seconds field.
* This is always positive, and never exceeds 999,999,999.
*/
private final int nanos;
...
}
An instantaneous point on the time-line.
This class models a single instantaneous point on the time-line. This might be used to record event time-stamps in the application.
java.time 패키지에 있는 클래스다.
Date 클래스와 비슷하게 epoch-second을 나타낸다. 차이점으로는 nanoseond 단위까지 알 수 있다.
@Test
void instant_test() {
Instant now = Instant.now();
System.out.println("now.getEpochSecond(): " + now.getEpochSecond());
System.out.println("now.getNano(): " + now.getNano());
}
// output
now.getEpochSecond(): 1709988238
now.getNano(): 462248000
LocalDate
@jdk.internal.ValueBased
public final class LocalDate
implements Temporal, TemporalAdjuster, ChronoLocalDate, Serializable {
/**
* The year.
*/
private final int year;
/**
* The month-of-year.
*/
private final short month;
/**
* The day-of-month.
*/
private final short day;
...
}
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.
LocalDate는 년도, 월, 일을 나타내는 불변 클래스이다.
@Test
void localDate_test() {
LocalDate now = LocalDate.now();
System.out.println(now);
}
// output
2024-03-09
LocalTime
@jdk.internal.ValueBased
public final class LocalTime
implements Temporal, TemporalAdjuster, Comparable<LocalTime>, Serializable {
/**
* The hour.
*/
private final byte hour;
/**
* The minute.
*/
private final byte minute;
/**
* The second.
*/
private final byte second;
/**
* The nanosecond.
*/
private final int nano;
...
}
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.
시간, 분, 초를 표현하는 불변 클래스이다.
@Test
void localTime_test() {
LocalTime now = LocalTime.now();
System.out.println(now);
}
// output
21:52:32.531443
LocalDateTime
@jdk.internal.ValueBased
public final class LocalDateTime
implements Temporal, TemporalAdjuster, ChronoLocalDateTime<LocalDate>, Serializable {
/**
* The date part.
*/
private final LocalDate date;
/**
* The time part.
*/
private final LocalTime time;
...
}
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.
위에서 정리한 LocalDate, LocalTime을 활용해 시간을 표현하는 불변 객체이다.
@Test
void localDateTime_test() {
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
}
//output
2024-03-09T21:55:04.354431
ZonedDateTime
@jdk.internal.ValueBased
public final class ZonedDateTime
implements Temporal, ChronoZonedDateTime<LocalDate>, Serializable {
/**
* The local date-time.
*/
private final LocalDateTime dateTime;
/**
* The offset from UTC/Greenwich.
*/
private final ZoneOffset offset;
/**
* The time-zone.
*/
private final ZoneId zone;
...
}
ZonedDateTime is an immutable representation of a date-time with a time-zone. This class stores all date and time fields, to a precision of nanoseconds, and a time-zone, with a zone offset used to handle ambiguous local date-times. For example, the value "2nd October 2007 at 13:45.30.123456789 +02:00 in the Europe/Paris time-zone" can be stored in a ZonedDateTime.
LocalDateTime에 TimeZone 개념을 추가로 표현하는 객체이다.
쉽게 설명하면 LocalDateTime은 UTC 기준으로 시간을 표현한다면, ZonedDateTime에서는 어디 TimeZone(ZoneId)을 사용하고 UTC 기준 시간과 얼마나 차이가(ZoneOffset) 나는지 알 수 있다.
@Test
void zonedDateTime_test() {
ZonedDateTime now = ZonedDateTime.now();
System.out.println(now);
}
// output
2024-03-09T21:59:14.145559+09:00[Asia/Seoul]
정리
지금까지 Java에서 자주 사용되는 시간관련 클래스를 정리해 보았다.
만약 JDK 1.8 이상을 사용한다면, Date 보다는 java.time 패키지에 있는 클래스를 사용하는 것을 추천한다. 다양한 편의 메소드가 존재한다.
글로벌 서비스를 개발해 TimeZone이 중요하다면 LocalDateTime 보다 ZonedDateTime을 이용하는 것을 추천한다.
'Java' 카테고리의 다른 글
SimpleDateForat 동시성 이슈 (1) | 2025.02.10 |
---|---|
람다식 (0) | 2022.05.29 |
익명 클래스 (0) | 2022.05.15 |
Iterator (0) | 2022.04.24 |