Ch10-4~5. Calendar 클래스
0. 목차
Chapter10. 날짜와 시간 & 형식화
Ch10 - 1. Calendar 클래스 예제2
Ch10 - 2. Calendar 클래스 예제3
Ch10 - 4. Calendar 클래스 예제2
▶ set()으로 날짜와 시간 지정 가능
▷ 특정 필드만 지정
void set(int field, int value)
▷ 년, 월, 일 지정
void set(int year, int month, int date)
- 한 번에 년, 월, 일 설정
Calendar cal = Calendar.getInstance(); cal.set(2021, 6, 23); // 2021년 7월 23일
- 하나 씩 년, 월, 일 설정
Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, 2021); // 2021년 cal.set(Calendar.MONTH, 6); // 7월 cal.set(Calendar.DATE, 23); // 23일
월(MONTH) 지정 시, 0부터 시작한다는 점에 주의(해당 월 -1로 입력)
▷ 년, 월, 일, 시, 분, 초 지정
void set(int year, int month, int date, int hourOfDay, int minute, int second)
Calendar time = Calendar.getInstance();
time.set(Calendar.HOUR_OF_DAY, 10); // 10시
time.set(Calendar.MINUTE, 20); // 20분
time.set(Calendar.SECOND, 30); // 30초
▶ 실습1
▷ 2021년 7월 31일
public static void main(String[] args) {
Calendar time = Calendar.getInstance();
time.set(2021, 6, 31);
System.out.println(time.get(Calendar.YEAR));
System.out.println(time.get(Calendar.MONTH) + 1);
System.out.println(time.get(Calendar.DATE));
}
// console
2021
7
31
▷ 토요일 1 : ‘7’로 출력
System.out.println(time.get(Calendar.DAY_OF_WEEK));
// console
7
▷ 토요일 2 : “토”로 출력
final String[] DAY_OF_WEEK = {"", "일", "월", "화", "수", "목", "금", "토"};
System.out.println(DAY_OF_WEEK[time.get(Calendar.DAY_OF_WEEK)]);
// console
토
- 2021년 7월 31일 토
final String[] DAY_OF_WEEK = {"", "일", "월", "화", "수", "목", "금", "토"}; public static String when(Calendar t) { return t.get(Calendar.YEAR) + "년 " + (t.get(Calendar.MONTH) + 1) + "월 " + (t.get(Calendar.DATE)) + "일 "; } public static void main(String[] args) { C c = new C(); Calendar time = Calendar.getInstance(); time.set(2021, 6, 31); System.out.println(when(time) + c.DAY_OF_WEEK[time.get(Calendar.DAY_OF_WEEK)]); } // console 2021년 7월 31일 토
▷ 1월 1일 부터 지금까지 몇일이 지났는가? 1 : 초(second)단위
7월 31일 - 1월 1일
- For 기계 : 초(second)단위로 환산해서 뺄셈
getTimeInMillis() = 1/1000초
getTimeInMillis()/1000 = 1초
atTime.set(2021, 0, 1); long secondDifference = (time.getTimeInMillis() - atTime.getTimeInMillis()) / 1000; System.out.println(secondDiffrence); // console 18230399 // (second)time - (second)atTime
▷ 1월 1일 부터 지금까지 몇일이 지났는가? 2 : 일(day)단위
- For 사람 : 일(day)단위로 환산해서 출력
- 시간 환산 ≒ 거스름 돈
- ex) 3790원 동전으로 다 주기
- 500원
3790 ÷ 500 = 7
3790 – 3500 = 290
- 100원
290 ÷ 100 = 2
290 – 200 = 90
- 50원
90 ÷ 50 = 1
90 – 50 = 40
- 10원
40 ÷ 10 = 4
- 500원
- 시간 환산
1 × 60 = 60초 = 1분
1분 × 60 = 1시간
1시간 × 24 = 1일
atTime.set(2021, 1, 1); long secondDifference = (time.getTimeInMillis() - atTime.getTimeInMillis()) / 1000; long dayDifference = secondDiffrence / (24 * 60 * 60); System.out.println(secondDifference); System.out.println(dayDifference); // console 15551999 // (second)time - (second)atTime 210 // (day)time - (day)atTime
▷ 전체
package baek;
import java.util.Calendar;
class C {
C() { }
final String[] DAY_OF_WEEK = {"", "일", "월", "화", "수", "목", "금", "토"};
public static String when(Calendar t) {
return t.get(Calendar.YEAR) + "년 " +
(t.get(Calendar.MONTH) + 1) + "월 " +
(t.get(Calendar.DATE)) + "일 ";
}
public static void main(String[] args) {
C c = new C();
Calendar time = Calendar.getInstance();
Calendar atTime = Calendar.getInstance();
Calendar lastTime = Calendar.getInstance();
time.set(2021, 6, 31);
atTime.set(2021, 0, 1);
lastTime.set(2022, 0, 1);
long secondDifference = (time.getTimeInMillis() - atTime.getTimeInMillis()) / 1000;
long hourDifference = secondDifference / (60 * 60);
long dayDifference = secondDifference / (24 * 60 * 60);
long remainSecondDifference = (lastTime.getTimeInMillis() - time.getTimeInMillis()) / 1000;
long remainHourDifference = remainSecondDifference / (60 * 60);
long remainDayDifference = remainSecondDifference / (24 * 60 * 60);
System.out.println(when(time) + c.DAY_OF_WEEK[time.get(Calendar.DAY_OF_WEEK)]);
System.out.println();
System.out.println(when(atTime) + "-d-o-n-e→ " + when(time));
System.out.println(when(atTime) + "-" + hourDifference + "시간→ " + when(time));
System.out.println(when(atTime) + "-" + dayDifference + "일→ " + when(time));
System.out.println();
System.out.println((time.get(Calendar.YEAR) + 1) + "년 까지 남은 시간은?");
System.out.println(when(time) + "-re-m-a-i-n→ " + when(lastTime));
System.out.println(when(time) + "-" + remainHourDifference + "시간→ " + when(lastTime));
System.out.println(when(time) + "-" + remainDayDifference + "일→ " + when(lastTime));
}
}
// console
2021년 7월 31일 토
2021년 1월 1일 -d-o-n-e→ 2021년 7월 31일
2021년 1월 1일 -5063시간→ 2021년 7월 31일
2021년 1월 1일 -210일→ 2021년 7월 31일
2022년 까지 남은 시간은?
2021년 7월 31일 -re-m-a-i-n→ 2022년 1월 1일
2021년 7월 31일 -3696시간→ 2022년 1월 1일
2021년 7월 31일 -154일→ 2022년 1월 1일
▶ 실습2
▷ timeOne 20시 20분 30초
▷ timeTwo 10시 20분 30초
public static String bedTime(Calendar n) {
return n.get(Calendar.HOUR_OF_DAY) + ":" + n.get(Calendar.MINUTE) + ":" + n.get(Calendar.SECOND);
}
public static void main(String[] args) {
Calendar timeOne = Calendar.getInstance();
Calendar timeTwo = Calendar.getInstance();
timeOne.set(Calendar.HOUR_OF_DAY, 20);
timeOne.set(Calendar.MINUTE, 20);
timeOne.set(Calendar.SECOND, 30);
timeTwo.set(Calendar.HOUR_OF_DAY, 10);
timeTwo.set(Calendar.MINUTE, 20);
timeTwo.set(Calendar.SECOND, 30);
System.out.println("[TimeOne] " + time(timeOne));
System.out.println("[TimeTwo] " + time(timeTwo));
}
// console
[TimeOne] 20:20:30
[TimeTwo] 10:20:30
▷ timeOne 20:20:30
■■■■■■■■■■■■■■■■■■■■□□□□
▷ timeOne 10:20:30
■■■■■■■■■■□□□□□□□□□□□□□□
▷ TimeOne - TimeTwo
구하기
■■■■■■■■■■■■■■■■■■■■□□□□
-
■■■■■■■■■■□□□□□□□□□□□□□□
- 초(second) 단위로 환산 후 뺄셈
TimeOne.getTimeInMillis()/1000 - TimeTwo.getTimeInMillis()/1000
- n시간, n분, n초 단위로 환산
초/1 = 초
초/60 = 분(1초 × 60)
초/60 * 60 = 시간(1분 × 60)
final int[] TIME_UNIT = {3600, 60, 1}; final String[] TIME_UNIT_NAME = {"시간 ", "분 ", "초"}; long difference = (timeOne.getTimeInMillis() - timeTwo.getTimeInMillis())/1000; String tmp = ""; for (int i = 0; i < TIME_UNIT.length; i++) { tmp += difference/TIME_UNIT[i] + TIME_UNIT_NAME[i]; difference %= TIME_UNIT[i]; } System.out.println("timeOne - timeTwo = " + tmp); // console timeOne - timeTwo = 9시간 59분 59초
▷ 전체
class SleepTime {
SleepTime() { }
public static String time(Calendar t) {
return t.get(Calendar.HOUR_OF_DAY) + ":" +
t.get(Calendar.MINUTE) + ":" +
t.get(Calendar.SECOND);
}
public static void main(String[] args) {
Calendar timeOne = Calendar.getInstance();
Calendar timeTwo = Calendar.getInstance();
timeOne.set(Calendar.HOUR_OF_DAY, 20);
timeOne.set(Calendar.MINUTE, 20);
timeOne.set(Calendar.SECOND, 30);
timeTwo.set(Calendar.HOUR_OF_DAY, 10);
timeTwo.set(Calendar.MINUTE, 20);
timeTwo.set(Calendar.SECOND, 30);
System.out.println("[TimeOne] " + time(timeOne));
System.out.println("[TimeTwo] " + time(timeTwo));
final int[] TIME_UNIT = {3600, 60, 1};
final String[] TIME_UNIT_NAME = {"시간 ", "분 ", "초"};
long difference = (timeOne.getTimeInMillis() - timeTwo.getTimeInMillis())/1000;
String tmp = "";
for (int i = 0; i < TIME_UNIT.length; i++) {
tmp += difference/TIME_UNIT[i] + TIME_UNIT_NAME[i];
difference %= TIME_UNIT[i];
}
System.out.println("timeOne - timeTwo = " + tmp);
}
}
// console
[TimeOne] 20:20:30
[TimeTwo] 10:20:30
timeOne - timeTwo = 9시간 59분 59초
Ch10 - 5. Calendar 클래스 예제3
▶ clear()란?
▷ clear()는 Calendar 객체의 모든 필드를 초기화
▷ Epoch Time : 1970년 1월 1일 00:00:00로 초기화
Calendar cal = Calendar.getInstance(); // 현재 시간으로
System.out.println(new Date(cal.getTimeInMillis()));
// console
Sat Jul 24 17:58:20 KST 2021
cal.clear(); // Epoch Time으로 초기화 됨
System.out.println(new Date(cal.getTimeInMillis()));
// console
Thu Jan 01 00:00:00 KST 1970
▷ clear(int field)는 Calendar 객체의 특정 필드를 초기화
Calendar cal = Calendar.getInstance();
cal.clear(Calendar.SECOND); // 초를 초기화 : 0초
cal.clear(Calendar.MINUTE); // 분을 초기화 : 0분
cal.clear(Calendar.HOUR_OF_DAY); // 시간을 초기화 : 0시간
cal.clear(Calendar.HOUR); // 시간을 초기화 : 0시간
System.out.println(new Date(cal.getTimeInMillis()));
// console
Mon Jul 26 00:00:00 KST 2021
▶ 객체 생성 → clear → set : 이렇게 해야 계산이 정확
▷ yyyymmdd1 - yyyymmdd2
20200103 - 20200101 = 2일
static int getDayDiff(String yyyymmdd1, String yyyymmdd2) {
int diff = 0;
try {
int year1 = Integer.parseInt(yyyymmdd1.substring(0, 4));
int month1 = Integer.parseInt(yyyymmdd1.substring(4, 6)) - 1;
int day1 = Integer.parseInt(yyyymmdd1.substring(6, 8));
int year2 = Integer.parseInt(yyyymmdd2.substring(0, 4));
int month2 = Integer.parseInt(yyyymmdd2.substring(4, 6)) - 1;
int day2 = Integer.parseInt(yyyymmdd2.substring(6, 8));
Calendar date1 = Calendar.getInstance();
Calendar date2 = Calendar.getInstance();
date1.set(year1, month1, day1);
date2.set(year2, month2, day2);
diff = (int)(date1.getTimeInMillis() - date2.getTimeInMillis())/(24 * 60 * 60 * 1000);
} catch (Exception e) {
diff = 0;
}
return diff;
}
public static void main(String[] args) {
System.out.println(getDayDiff("20200103", "20200101"));
}
// console
1
20200103 - 20200101 =
2일
1일?
Calendar date1 = Calendar.getInstance();
Calendar date2 = Calendar.getInstance();
date1.set(year1, month1, day1);
date2.set(year2, month2, day2);
System.out.println((date1.getTimeInMillis() - date2.getTimeInMillis()) / (24 * 60 * 60 * 1000f));
// console
1.9999998
yyyymmdd1과 yyyymmdd2의 차이는 2에서 0.00000002 부족
왜?
date1과 date2 객체 생성 시 차이가 발생
그래서 clear()가 필요!
Calendar date1 = Calendar.getInstance();
Calendar date2 = Calendar.getInstance();
date1.clear();
date2.clear();
date1.set(year1, month1, day1);
date2.set(year2, month2, day2);
System.out.println((date1.getTimeInMillis() - date2.getTimeInMillis()) / (24 * 60 * 60 * 1000f));
// console
2.0
clear() 생성 후
date1과 date2 객체 생성 시 차이X