로컬 날짜 클래스
package Java;
import java.time.LocalDate;
public class HelloJava {
public static void main(String[] args) {
LocalDate nowDate = LocalDate.now();
System.out.println(nowDate.getYear()); // 2024
System.out.println(nowDate.getMonthValue()); // 7
System.out.println(nowDate.getDayOfMonth()); // 27
System.out.println(nowDate.getDayOfYear()); // 209
System.out.println(nowDate.getDayOfWeek()); // SATURDAY
System.out.println(nowDate.getDayOfWeek().getValue()); // 6
System.out.println(nowDate.lengthOfMonth()); // 31
System.out.println(nowDate.lengthOfYear()); // 366
}
}
로컬 시간 클래스
package Java;
import java.time.LocalTime;
public class HelloJava {
public static void main(String[] args) {
LocalTime time = LocalTime.of(1,5,8,123456789);
System.out.println(time.getHour()); // 1
System.out.println(time.getMinute()); // 5
System.out.println(time.getSecond()); // 8
System.out.println(time.getNano()); // 123456789
}
}
로컬 날짜 및 시간 클래스 (LocalDate + LocalTime)
package Java;
import java.time.LocalDateTime;
public class HelloJava {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
System.out.println(now.getYear()); // 2024
System.out.println(now.getMonthValue()); // 7
System.out.println(now.getDayOfMonth()); // 27
System.out.println(now.getDayOfYear()); // 209
System.out.println(now.getDayOfWeek()); // SATURDAY
System.out.println(now.getDayOfWeek().getValue()); // 6
System.out.println(now.getHour()); // 1
System.out.println(now.getMinute()); // 5
System.out.println(now.getSecond()); // 8
System.out.println(now.getNano()); // 123456789
}
}
특정 타임존(TimeZone)의 날짜와 시간 클래스
2014-04-21T07:50:24.017+09:00[Asia/Seoul]
처럼 타임존 정보가 추가
package Java;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class HelloJava {
public static void main(String[] args) {
LocalDateTime dateTime = LocalDateTime.now();
System.out.println(dateTime); // 2024-07-28T15:25:12.047011100
ZonedDateTime zoneDateTime = ZonedDateTime.now();
System.out.println(zoneDateTime); // 2024-07-28T15:25:12.047011100+09:00[Asia/Seoul]
ZonedDateTime utcDateTime = zoneDateTime.withZoneSameInstant(ZoneId.of("UTC"));
System.out.println(utcDateTime); // 2024-07-28T06:25:12.047011100Z[UTC]
}
}
특정 시점의 Time-Stamp 클래스
주로 특정한 두 시점 간의 시간적 우선순위를 따질 때 사용
package Java;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
public class HelloJava {
public static void main(String[] args) {
Instant instant1 = Instant.now();
Instant instant2 = Instant.now();
if(instant1.isBefore(instant2)) {
System.out.println("instant1이 빠릅니다.");
} else if(instant1.isAfter(instant2)) {
System.out.println("instant1이 늦습니다.");
} else {
System.out.println("동일한 시간입니다.");
}
// 두 시간의 차 구하기
System.out.println("차이(nanos) : " + instant1.until(instant2, ChronoUnit.NANOS));
}
}
메소드 | 설명 | |
---|---|---|
LocalDate now() | 현재 날짜로 LocalDate 인스턴스 | |
LocalDate parse(CharSequence text) | DateTimeFormatter에는 표준화 된 포맷터를 이용하여 LocalDate 반환 | |
LocalDate parse(CharSequence text, DateTimeFormatter formatter) | 커스텀 된 DateTimeFormatter 포맷터를 이용하여 LocalDate 반환 | |
LocalDate of(int year, int month, int dayOfMonth) | ||
LocalDate of(int year, Month month, int dayOfMonth) | 설정한 날짜로 LocalDate 인스턴스 | |
int getYear() | 연도(2024) | |
int getMonthValue() | 월(1~12) | |
int getDayOfMonth() | 일(1~31) | |
int getDayOfYear() | 해당 해의 1월 1일부터 몇번째 일(365) | |
DayOfWeek getDayOfWeek() | 요일(SUNDAY~SATURDAY) | |
getDayOfWeek().getValue() 를 통해 0(일)~6(토)로 획득 가능 | ||
int lengthOfMonth() | 해당 달의 총 일수 | |
int lengthOfYear() | 해당 해의 총 일수 |
메소드 | 설명 |
---|---|
LocalTime now() | 현재 시간으로 LocalTime 인스턴스 |
LocalTime parse(CharSequence text) | DateTimeFormatter에는 표준화 된 포맷터를 이용하여 LocalTime 반환 |
LocalTime parse(CharSequence text, DateTimeFormatter formatter) | 커스텀 된 DateTimeFormatter 포맷터를 이용하여 LocalTime 반환 |
LocalTime of(int hour, int minute) | |
LocalTime of(int hour, int minute, int second) LocalTime of(int hour, int minute, int second, int nanoOfSecond) | 설정한 시간으로 LocalTime 인스턴스 |
int get Hour() | 시(0~23) |
int getMinute() | 분(0~59) |
int getSecond() | 초(0~59) |
int geetNano() | 나노초(0~999999999) |
메소드 | 설명 |
---|---|
LocalDateTime now() | 현재 날짜로 LocalDate 인스턴스 |
LocalDateTime parse(CharSequence text) | DateTimeFormatter에는 표준화 된 포맷터를 이용하여 LocalDateTime 반환 |
LocalDateTime parse(CharSequence text, DateTimeFormatter formatter) | 커스텀 된 DateTimeFormatter 포맷터를 이용하여 LocalDateTime 반환 |
LocalDateTime of(int year, int month, int dayOfMonth) | 설정한 날짜로 LocalDate 인스턴스 |
int getYear() | 연도(2024) |
int getMonthValue() | 월(1~12) |
int getDayOfMonth() | 일(1~31) |
int getDayOfYear() | 같은 해의 1월 1일부터 몇번째 일(365) |
DayOfWeek getDayOfWeek() | 요일(SUNDAY~SATURDAY) |
getDayOfWeek().getValue() 를 통해 0(일)~6(토)로 획득 가능 | |
int get Hour() | 시(0~23) |
int getMinute() | 분(0~59) |
int getSecond() | 초(0~59) |
int geetNano() | 나노초(0~999999999) |
LocalDate toLocalDate() | LocalDate 타입으로 반환 |
LocalTime toLocalTime() | LocalTime 타입으로 반환 |
메소드 | 설명 |
---|---|
ZonedDateTime now()ZonedDateTime now(ZoneId zone) | |
ZonedDateTime now(Clock clock) | Zone Date Time 인스턴스 생성 |
ZonedDateTime parse(CharSequence text) | DateTimeFormatter에는 표준화 포맷터를 이용하여 ZonedDateTime 반환 |
ZonedDateTime parse(CharSequence text, DateTimeFormatter formatter) | 커스텀 된 DateTimeFormatter 포맷터를 이용하여 ZonedDateTime 반환 |
ZonedDateTime withZoneSameInstant(ZoneId zone) | zone 기준시로 DateTime 생성 |
ZoneId getZone() | Zone ID 반환 |
LocalDate
, LocalDateTime
, ZonedDateTime
에서 모두 사용 가능
리턴 타입은 각 클래스와 동일
메소드 | 설명 |
---|---|
withYear(int year) | 연 변경 |
withMonth(int month) | 월 변경 |
withDayOfMonth(int dayOfMonth) | 일 변경 |
withDayOfYear(int dayOfYear) | 해당 연의 첫날 기준, dayOfYear일 후로 변경 |
withHour(int hour) | 시 변경 |
withMinute(int minute) | 분 변경 |
withSecond(int second) | 초 변경 |
withNano(int nano) | 나노초 변경 |
with(TemporalAdjuster.Method()) | TemporalAdjuster의 Static Method를 이용하여 날짜 및 시간을 상대 변경 |
TemporalAdjusters
Static Methods
메소드 | 설명 |
---|---|
firstDayOfYear() | 해당 해의 첫 번째 일 |
lastDayOfYear() | 해당 해의 마지막 일 |
firstDayOfMonth() | 해당 달의 첫 번째 일 |
lastDayOfMonth() | 해당 달의 마지막 일 |
firstDayOfNextYear() | 다음 해의 첫 번째 일 |
firstDayOfNextMonth() | 다음 달의 첫 번째 일 |
firstInMonth(DayOfWeek dayOfWeek) | 해당 요일의 그 달 첫 번째 요일 |
lastInMonth(DayOfWeek dayOfWeek) | 해당 요일의 그 달 마지막 요일 |
next(DayOfWeek dayOfWeek) | 돌아오는 요일 |
nextOrSame(DayOfWeek dayOfWeek) | 오늘 포함 돌아오는 요일 |
previous(DayOfWeek dayOfWeek) | 지난 요일 |
previousOrSame(DayOfWeek dayOfWeek) | 오늘 포함 지난 요일 |
LocalDate 메소드
메소드 | 설명 |
---|---|
boolean isBefore(ChronoLocalDate other) | other보다 이전 날짜인지 판별 후 리턴 |
boolean isAfter(ChronoLocalDate other) | other보다 이후 날짜인지 판별 후 리턴 |
boolean isEqual(ChronoLocalDate other) | other과 동일 날짜인지 판별 후 리턴 |
Period until(ChronoLocalDate endDateExclusive) | endDateExclusive와 날짜의 차이를 반환 |
예) 2020-01-01 과 2024-07-28일 경우 27 반환 | |
long until(Temporal endExclusive, TemporalUnit unit) | 설정한 unit 단위로 날짜 및 시간의 차이를 반환 |
ChronoUnit unit은 DAYS, WEEKS, MONTHS, YEARS, DECADES, CENTURIES, MILLENNIA , ERAS 를 지원
예) 2020-01-01 과 2024-07-28일을 DAYS로 차이를 구할 경우 1670 반환 |
LocalTime 메소드
메소드 | 설명 |
---|---|
String format(DateTimeFormatter formatter) | DateTimeFormatter 형식대로 문자열을 리턴 |
메소드 | 설명 |
---|---|
String format(DateTimeFormatter formatter) | DateTimeFormatter 형식대로 문자열을 리턴 |
메소드 | 설명 |
---|---|
String format(DateTimeFormatter formatter) | DateTimeFormatter 형식대로 문자열을 리턴 |
메소드 | 설명 |
---|---|
String format(DateTimeFormatter formatter) | DateTimeFormatter 형식대로 문자열을 리턴 |