본문 바로가기

JAVA/JAVA 공부

calendar

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
상단에 
import java.util.Calendar; 추가
 
int year; 
int month; 
 
System.out.print("연도 = "); 
year = sc.nextInt(); 
 
System.out.print("월 = "); 
month = sc.nextInt(); 
 
Calendar cal = Calendar.getInstance(); // Calendar 선언 
 
// 년 월 일 달력 선언 
cal.set(Calendar.YEAR, year); 
cal.set(Calendar.MONTH, month - 1);  
cal.set(Calendar.DATE, 1); // 1일 부터 시작 
 
int startDay = cal.get(Calendar.DAY_OF_WEEK); 
int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); 
int day = 1
 
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("====================================================="); 
 
// 위쪽 빈칸 
for (int i = 1; i < startDay; i++) { 
System.out.print("*" + "\t"); 
 
// 날짜 
for (int i = 0; i < lastDay; i++) { 
System.out.print(day + "\t"); 
 
if((day + startDay - 1) % == 0) { 
System.out.println(); 
 
day++
 
// 밑 빈칸 
for (int i = 0; i < (- (startDay + lastDay - 1) % 7); i++) { 
System.out.print("*" + "\t"); 
System.out.println(); 
 
System.out.println("====================================================="); 
 
}
 
 
----------------------------------------------------------------------------------
 
 
import java.util.Calendar; 
import java.util.GregorianCalendar; 
 
public class myClass { 
 
public static void main(String[] args) { 
 
// Calendar == 달력 
// Calendar cal = new GregorianCalendar(); 
 
Calendar cal = Calendar.getInstance(); 
 
// 취득 get 
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 + "일"); 
 
// 설정 set 
/* 
cal.set(Calendar.YEAR, 2019); 
cal.set(Calendar.MONTH, Calendar.JANUARY); 
cal.set(Calendar.DATE, 23); 
year = cal.get(Calendar.YEAR); 
month = cal.get(Calendar.MONTH) + 1; 
day = cal.get(Calendar.DATE); 
System.out.println(year + "년 " + month + "월 " + day + "일"); 
*/ 
 
String ampm = cal.get(Calendar.AM_PM)==0?"오전":"오후"
System.out.println("ampm = " + ampm); 
 
// 요일 
int weekDay = cal.get(Calendar.DAY_OF_WEEK);  // 1 ~ 7 
System.out.println("weekDay = " + weekDay); 
 
switch (weekDay) { 
case 1
System.out.println("일요일"); 
break
case 2
System.out.println("월요일"); 
break
case 3
System.out.println("화요일"); 
break
case 4
System.out.println("수요일"); 
break
case 5
System.out.println("목요일"); 
break
case 6
System.out.println("금요일"); 
break
case 7
System.out.println("토요일"); 
break
 
cal.set(Calendar.YEAR, 2020); 
cal.set(Calendar.MONTH, - 1);  
// 지정한 달의 마지막 날짜 == 28, 29, 30, 31 
int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); 
System.out.println("lastDay = " + lastDay); 
 
 
// 연월일을 설정하면 달력의 빈칸이 몇개있는지 
year = 2018
month = 7
day = 1
 
cal.set(Calendar.YEAR, year); 
cal.set(Calendar.MONTH, month - 1); 
cal.set(Calendar.DATE, day); 
 
// 요일 
weekDay = cal.get(Calendar.DAY_OF_WEEK); // 1 ~ 7 
 
int _day = (weekDay - 1) % 7
System.out.println(month + "월의 윗쪽의 빈칸 수 = " + _day); 
 
lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); 
cal.set(Calendar.DATE, lastDay); 
 
// 요일 
weekDay = cal.get(Calendar.DAY_OF_WEEK);  
System.out.println("weekDay = " + weekDay); 
 
int _lastday = - weekDay; 
System.out.println("이 달의 밑쪽의 빈칸 수 = " + _lastday); 
 
 
 
 
cs

'JAVA > JAVA 공부' 카테고리의 다른 글

아스키코드 사진  (1) 2018.10.05
(한번에보기)배열개념,정렬,검색 알아보자  (0) 2018.07.01
File 생성(2)  (0) 2018.06.26
File 생성(1)  (0) 2018.06.26
randomClass  (0) 2018.06.26