-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrintCalendar.java
65 lines (48 loc) · 1.56 KB
/
PrintCalendar.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
public class PrintCalendar {
public static void main(String[] agrs) {
Scanner input = new Scanner(System.in);
System.out.print("Enter full year (e.g., 2016): ");
int year = input.nextInt();
System.out.print("Enter month as a number between 1 and 12: ");
int month = input.nextInt();
printCalendar(year, month);
}
public static void printCalendar(int year, int month) {
System.out.print(month + " " + year);
}
public static void printMonthTitle(int year, int month) {
}
public static void printMonthBody(int year, int month) {
}
public static String getMonthName(int month) {
return "January";
}
public static int getStartDay(int month) {
final int START_DAY_FOR_JAN_1_1800 = 3;
int totalNumberOfDays = getTotalNumberOfDays(year, month);
return (totalNumberOfDays + START_DAY_FOR_JAN_1_1800) % 7;
}
public static int getTotalNumberOfDays(int year, int month) {
int total = 0;
for (int = 1800; i < year; i++)
if (isLeapYear(i))
total += 366;
else
total += 365;
for (i = 1; i < month; i++)
total += getNumberOfDaysInMonth(year, i);
return total;
}
public static int getNumberOfDaysInMonth(int year, int month) {
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month ==12)
return 31;
if (month == 4 || month == 6 || month == 9 || month ==11)
return 30;
if (month == 2)
return isLeapYear(year) ? 29 : 28;
return 0;
}
public static boolean isLeapYear(int year) {
return year % 400 == 0 || (year % 4 ==0 && year % 100 !=0);
}
}