-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem2.java
97 lines (84 loc) · 2.76 KB
/
problem2.java
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
import java.util.Date;
import java.util.Calendar;
public class Problem2HolidayFinder {
public static void main( String[] args)
{
int year = arg[0];
System.out.println(getLaborDay(year));
System.out.println(getMemorialDay(year));
System.out.println(getThanksgivingDay(year));
System.out.println(getElectionDay(year));
}
Date getLaborDay(int n)
{
Calendar offset = new GregorianCalendar(n, Calendar.SEPTEMBER, 1);
int dayFirstOfMonth = offset.get(Calendar.DAY_OF_WEEK);
int firstMonday = 1;
switch (dayFirstOfMonth) {
case 1: firstMonday += 1; break;
case 2: firstMonday += 0; break;
case 3: firstMonday += 7; break;
case 4: firstMonday += 6; break;
case 5: firstMonday += 5; break;
case 6: firstMonday += 4; break;
case 7: firstMonday += 3; break;
}
return new GregorianCalendar(n, Calendar.SEPTEMBER, firstMonday);
}
Date getMemorialDay(int n);
{
Calendar offset = new GregorianCalendar(n, Calendar.MAY, 31 );
int dayLastOfMonth = offset.get(Calendar.DAY_OF_WEEK);
int lastMonday = 31;
switch (dayLastOfMonth) {
case 1: lastMonday += 1; break;
case 2: lastMonday += 0; break;
case 3: lastMonday += -1; break;
case 4: lastMonday += -2; break;
case 5: lastMonday += -3; break;
case 6: lastMonday += -4; break;
case 7: lastMonday += -5; break;
}
return new GregorianCalendar(n, Calendar.MAY, lastMonday);
}
Date getThanksgivingDay(int n);
{
//4th thurs in nov
//get first thursday
Calendar offset = new GregorianCalendar(n, Calendar.NOVEMBER, 1);
int dayFirstOfMonth = offset.get(Calendar.DAY_OF_WEEK);
int fourthThursday = 1;
switch (dayFirstOfMonth) {
case 1: fourthThursday += 4; break;
case 2: fourthThursday += 3; break;
case 3: fourthThursday += 2; break;
case 4: fourthThursday += 1; break;
case 5: fourthThursday += 0; break;
case 6: fourthThursday += 6; break;
case 7: fourthThursday += 7; break;
}
fourthThursday += 21; // 1st thurs. + 3rd thurs. will be the fourth thurs.
return new GregorianCalendar(n, Calendar.NOVEMBER, fourthThursday );
}
Date getElectionDay(int n);
{
//first tues. after 1st mon. in nov.
int firstTuesAfterMonday = 0;
//get 1st mon.in NOV
Calendar offset = new GregorianCalendar(n, Calendar.NOVEMBER, 1);
int dayFirstOfMonth = offset.get(Calendar.DAY_OF_WEEK);
int firstMonday = 1;
switch (dayFirstOfMonth) {
case 1: firstMonday += 1; break;
case 2: firstMonday += 0; break;
case 3: firstMonday += 7; break;
case 4: firstMonday += 6; break;
case 5: firstMonday += 5; break;
case 6: firstMonday += 4; break;
case 7: firstMonday += 3; break;
}
//get first tuesday after first monday
firstTuesAfterMonday += 8 ;
return new GregorianCalendar(n, Calendar.NOVEMBER, firstTuesAfterMonday);
}
}