|
@@ -0,0 +1,70 @@
|
|
1
|
+Quiz. 20160101와 20160103을 입력하면
|
|
2
|
+20160101
|
|
3
|
+20160102
|
|
4
|
+20160103
|
|
5
|
+을 출력하는 코드를 작성하세요.
|
|
6
|
+
|
|
7
|
+// 입력한 달이 몇일까지 있는지를 계산
|
|
8
|
+public static int endDayFromTotalDay(int year, int month){
|
|
9
|
+ int lastday;
|
|
10
|
+ switch (month) {
|
|
11
|
+ case 2:
|
|
12
|
+ lastday = 28;
|
|
13
|
+ if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
|
|
14
|
+ lastday = 29;
|
|
15
|
+ else
|
|
16
|
+ lastday = 28;
|
|
17
|
+ break;
|
|
18
|
+ case 4:
|
|
19
|
+ case 6:
|
|
20
|
+ case 9:
|
|
21
|
+ case 11:
|
|
22
|
+ lastday = 30;
|
|
23
|
+ break;
|
|
24
|
+ default:
|
|
25
|
+ lastday = 31;
|
|
26
|
+ }
|
|
27
|
+ return lastday;
|
|
28
|
+}
|
|
29
|
+
|
|
30
|
+// 입력한 날짜의 총 날 수를 계산(기준은 1970.1.1부터)
|
|
31
|
+public static int totalDayFromCalendar(int year, int month, int day){
|
|
32
|
+ int totaldays;
|
|
33
|
+ totaldays = 365 * (year - 1);
|
|
34
|
+ for (int i = 1; i < year; i++) {
|
|
35
|
+ if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)
|
|
36
|
+ totaldays++;
|
|
37
|
+ }
|
|
38
|
+ // totaldays = 365 * (year-1) + (year-1)/4 - (year-1)/100 + (year-1)/400
|
|
39
|
+ int premonth = month - 1;
|
|
40
|
+ if (premonth >= 1)
|
|
41
|
+ totaldays += 31;
|
|
42
|
+ if (premonth >= 2)
|
|
43
|
+ totaldays += 28;
|
|
44
|
+ if (premonth >= 3)
|
|
45
|
+ totaldays += 31;
|
|
46
|
+ if (premonth >= 4)
|
|
47
|
+ totaldays += 30;
|
|
48
|
+ if (premonth >= 5)
|
|
49
|
+ totaldays += 31;
|
|
50
|
+ if (premonth >= 6)
|
|
51
|
+ totaldays += 30;
|
|
52
|
+ if (premonth >= 7)
|
|
53
|
+ totaldays += 31;
|
|
54
|
+ if (premonth >= 8)
|
|
55
|
+ totaldays += 31;
|
|
56
|
+ if (premonth >= 9)
|
|
57
|
+ totaldays += 30;
|
|
58
|
+ if (premonth >= 10)
|
|
59
|
+ totaldays += 31;
|
|
60
|
+ if (premonth >= 11)
|
|
61
|
+ totaldays += 30;
|
|
62
|
+ if (month > 2 && (year % 4 == 0 && year % 100 != 0 || year % 400 == 0))
|
|
63
|
+ totaldays++;
|
|
64
|
+ totaldays++;
|
|
65
|
+
|
|
66
|
+// int day = totaldays % 7;
|
|
67
|
+
|
|
68
|
+ totaldays = totaldays + day;
|
|
69
|
+ return totaldays;
|
|
70
|
+}
|