Java
날짜와 시간(Calendar class)
성장코딩
2022. 12. 26. 23:42
Theme. Calendar class
들어가기에 앞서, 현재 글을 적고 있는 시점의 날짜 (2022/12/26) 를 구해보자.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
package test;
import java.util.Calendar;
public class Practice {
public static void main(String[] args) {
// Calendar class: 일정관리, 회원관리, 인사관리
// Calendar class 객체 생성하는 방법
// Calendar cal = new GregorianCalendar();
// Calendar cal = Calendar.getInstance(); -> 이 방법을 주로 사용한다.
Calendar cal = Calendar.getInstance();
//오늘 날짜 취득 (getter)
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH) + 1; // get(Calendar.MONTH)의 결과가 0(1월) ~ 11(12월)이므로 +1 해주도록 한다.
int day = cal.get(Calendar.DATE);
System.out.println(year + "/" + month + "/" + day);
}
}
|
cs |
Calendar class 객체를 생성하는 방법은 주로 getInstance()를 사용하고,
get(Calendar.MONTH)의 결과는 0 ~ 11이므로 결과에 + 1을 하여 몇 월인지 쉽게 이해하도록 하는 것을 기억하자.
이번엔, 현재가 아닌 특정 시점의 날짜를 세팅해보자.
2023년 3월 26일을 세팅해보도록 한다.
이 역시 MONTH를 세팅할 때, 주의해라. 결과 값 2가 3월이다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
package test;
import java.util.Calendar;
public class Practice {
public static void main(String[] args) {
// 날짜를 직접 세팅 (setter)
Calendar cal = Calendar.getInstance();
// 2023년 3월 26일로 세팅해보자.
cal.set(Calendar.YEAR, 2023); //2023년
cal.set(Calendar.MONTH, 2); // 2(3월)!!
cal.set(Calendar.DATE, 26); //26일
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH) + 1;
int day = cal.get(Calendar.DATE);
System.out.println(year + "/" + month + "/" + day);
}
}
|
cs |
날짜를 세팅하게 되면 이후 코드들은 현재 날짜가 아닌 세팅된 날짜를 기준으로 실행되게 된다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
package test;
import java.util.Calendar;
public class Practice {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
// 오전/오후
String ampm = cal.get(Calendar.AM_PM) == 0 ? "오전":"오후"; // 삼항연산자 이용
System.out.println(ampm);
// 현재 요일
int weekday = cal.get(Calendar.DAY_OF_WEEK); // 1(일요일) ~ 7(토요일)
System.out.println(weekday);
// 달의 마지막 날
int lastday = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.println(lastday);
}
}
|
cs |
지난 2022년 11월 달력을 만들어보자.
달력을 만들기 위해, 2022년 11월 1일과 말일의 앞뒤 부분에 존재하는 빈칸들을 표현하기 위한 코드를 작성해보도록 한다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
package test;
import java.util.Calendar;
public class Practice {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
//2022년 11월 달력 만들기
//2022년 11월 1일로 세팅
cal.set(Calendar.YEAR, 2022); //2022년
cal.set(Calendar.MONTH, 11 - 1); //11월
cal.set(Calendar.DATE, 1); //1일
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH) + 1;
//11월의 말일은?
int lastdate = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
//11월 1일의 요일은?
int startday = cal.get(Calendar.DAY_OF_WEEK);
//달력 표현
System.out.println("< " + year + "년 " + month + "월 >");
System.out.println("=========================================================");
String week_day = "일월화수목금토";
for (int i = 0; i < week_day.length(); i++) {
char c = week_day.charAt(i);
System.out.print(c + "\t");
}
System.out.println();
System.out.println("=========================================================");
//11월 달력의 위쪽 빈칸
for (int i = 0; i < startday - 1; i++) {
System.out.print("*" + "\t");
}
//11월 1일부터 말일까지 채우기
int date = 1;
for (int i = 0; i < lastdate; i++) {
System.out.print(date + "\t");
if((date + startday -1) % 7 == 0 ) { // startday(화요일이므로 3) - 1 == 2
System.out.println(); // (startday - 1)을 통해 빈칸(*)을 채워주고
// 7로 나눠서 나누어 떨어지는 토요일마다 줄 바꿈
}
date++;
}
//12월 달력의 아래쪽 빈칸
//(startday - 1)로 빈칸을 고려하여 더해주고 이를 7로 나눈 나머지 만큼 마지막 주에 날짜가 채워지므로
// 빈칸은 (7 - 나눈 나머지)개
for (int j = 0; j < ( 7 - (startday - 1 + lastdate) % 7 ); j++) {
System.out.print("*" + "\t");
}
System.out.println();
System.out.println("=========================================================");
}
}
|
cs |
< console >
비교를 위해 2022년 11월 달력 사진을 첨부한다. (출처: 네이버 달력)