-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalendarr.java
More file actions
61 lines (46 loc) · 1.76 KB
/
Copy pathCalendarr.java
File metadata and controls
61 lines (46 loc) · 1.76 KB
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
package datastructures;
public class Calendarr
{
public static int day(int month, int day, int year) {
int y = year - (14 - month) / 12;
int x = y + y/4 - y/100 + y/400;
int m = month + 12 * ((14 - month) / 12) - 2;
int d = (day + x + (31*m)/12) % 7;
return d;
}
// return true if it is leap year.......
public static boolean isLeapYear(int year) {
if ((year % 4 == 0) && (year % 100 != 0)) return true;
if (year % 400 == 0) return true;
return false;
}
public static void main(String[] args)
{
int month = Integer.parseInt(args[0]); // month (Jan = 1, Dec = 12)
int year = Integer.parseInt(args[1]); // year
// months[i] = name of month i
String[] months =
{"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"};
// days[i] = number of days in month i
int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// check for leap year
if (month == 2 && isLeapYear(year)) days[month] = 29;
// print calendar week names as header.......
System.out.println(" " + months[month] + " " + year);
System.out.println(" S M Tu W Th F S");
// starting day of month
int d = day(month, 1, year);
// print the calendar
for (int i = 0; i < d; i++)
System.out.println(" ");
for (int i = 1; i <= days[month]; i++)
{
System.out.printf("%2",i);
if (((i + d) % 7 == 0) || (i == days[month]))
System.out.println();
}
}
}