JDK 1.0
public class Ex01 {
public static void main(String[] args) {
Date date = new Date();
System.out.println(date);
int year = date.getYear();
int month = date.getMonth(); // 0 ~ 11
System.out.println(year + " " + month);
long epochTime = date.getTime(); // 1970.1.1 자정, 1000분의 1초 단위로 카운팅
System.out.println(epochTime); // UID(Unique Id)를 만들때 자주 활용
}
}
JDK 1.1
설계가 빈약
static Calendar getInstance()
서기 달력외에 다른 체계 달력(불기)을 지역화 설정에 따라 객체 생성
int get(날짜 시간 필드) : 날짜, 시간 조회public class Ex02 {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
//System.out.println(cal);
printDate(cal);
}
public static void printDate(Calendar cal){
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH); //0 ~ 11
int day = cal.get(Calendar.DAY_OF_MONTH);
System.out.println(year + " " + month + " " + day);
}
}
void set(날짜 시간 필드, 값) : 날짜, 시간 변경 add(...) : 날짜 시간의 가감 👉 메서드명이 모호 : java.time 패키지 : plus(...), minus(...)public class Ex01 {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
printDate(cal);
//cal.set(Calendar.MONTH,11);
cal.add(Calendar.DAY_OF_MONTH, 150);
printDate(cal);
cal.add(Calendar.DAY_OF_MONTH, -100);
printDate(cal);
}
}
public class Ex02 {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
//cal.add(Calendar.MONTH,30);
cal.roll(Calendar.MONTH,30);
printDate(cal);
}
}
JDK8
인터페이스
날짜, 시간의 변경시 👉 새로운 객체 생성
LocalDate : 날짜
LocalDateTime atTime(시간....) : 날짜 + 시간
static LocalDate now() : 지금 현재 날짜
static LocalDate of(...) : 직접 지정한 날짜
public class Ex01 {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
System.out.println(today);
LocalDate date = LocalDate.of(2023,5,8);
System.out.println(date);
}
}
조회
int/long get(TemporalField...)
ChronoField
참고)
Locale : 지역화
변경
with(...) : 날짜 변경
plus(...) : 날짜 더하기
minus(...) : 날짜 빼기
public class Ex02 {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
int year = today.get(ChronoField.YEAR);
int month = today.get(ChronoField.MONTH_OF_YEAR);
int day = today.get(ChronoField.DAY_OF_MONTH);
int yoil = today.get(ChronoField.DAY_OF_WEEK);//요일 1(월)~7(일)
System.out.printf("%d, %d, %d, %d",year,month,day,yoil);
}
}
LocalTime : 시간
Instant : EpochTime - 1970. 1. 1 자정부터(UTC+0) 1/1000 단위 카운트 참고) Timestamp - 초 단위 카운팅
Duration : 시간 간격 (초, 나노 초)
public class Ex03 {
public static void main(String[] args) {
LocalTime now = LocalTime.now();
LocalTime endTime = LocalTime.of(17,50);
Duration du = Duration.between(now, endTime);
System.out.println(du);
}
}
Period : 날짜 사이의 간격(년, 월, 일)
betweenuntilpublic class Ex02 {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
LocalDate endDate = LocalDate.of(2024,9,30);
Period period = Period.between(endDate, today);
int month = period.getMonths();
int day = period.getDays();
System.out.println(-month + " " + -day);
}
}
형식화 클래스
DateTimeFomatter
public class Ex04 {
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm:ss E");
LocalDateTime startDate = LocalDateTime.of(2024,3,19,9,0);
String strDate = formatter.format(startDate);
System.out.println(strDate);
}
}핵심 클래스(LoacalDate,LocalTime,LocalDateTime...)
format(..) : 자바 객체 👉 형식화된 문자열 변경
parse(..) : 형식화된 문자열 👉 자바 객체
public class Ex05 {
public static void main(String[] args) {
String strDate = "05/09/24 15:16";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yy HH:mm");
LocalDateTime date = LocalDateTime.parse(strDate, formatter);
System.out.println(date);
}
}
날짜, 시간의 단위, 필드
TemporalField 인터페이스 - 필드
시간대 관련 클래스
ZoneId
ZoneOffset
특정 필드의 값 가져오기 - get(), getXXX()
int get(필드 명);
ChronoField : 날짜, 시간 필드
필드의 값 변경하기 - with(), plus(), minus()
LocalDate with() : 날짜/시간 변경
LocalDate plus() : 날짜/시간 +
LocalDate minus() : 날짜/시간 -
날짜와 시간의 비교 - isAfter(), isBefore(), isEqual()
- compareTo() : 음수 - isBefore()
- compareTo() : 0 - isEqual()
- compareTo() : 양수 - isAfter()
now() : 현재 날짜,시간
of(....)
java.time.format
형식화 문자열 -> 날짜/시간
DateTimeFormatter
DateTimeFormatter state ofPattern("패턴")
.format(TemporalAccessor ...)