Java Reference
In-Depth Information
59
public static void printMonthBody( int year, int month) {
printMonthBody
60
// Get start day of the week for the first date in the month
61
int startDay = getStartDay(year, month)
62
63
// Get number of days in the month
64
int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);
65
66 // Pad space before the first day of the month
67 int i = 0 ;
68 for (i = 0 ; i < startDay; i++)
69 System.out.print( " " );
70
71 for (i = 1 ; i <= numberOfDaysInMonth; i++) {
72 System.out.printf( "%4d" , i);
73
74 if ((i + startDay) % 7 == 0 )
75 System.out.println();
76 }
77
78 System.out.println();
79 }
80
81
/** Get the start day of month/1/year */
82
public static int getStartDay( int year, int month) {
getStartDay
83
final int START_DAY_FOR_JAN_1_1800 = 3 ;
84
// Get total number of days from 1/1/1800 to month/1/year
85
int totalNumberOfDays = getTotalNumberOfDays(year, month);
86
87
// Return the start day for month/1/year
88
return (totalNumberOfDays + START_DAY_FOR_JAN_1_1800) % 7 ;
89 }
90
91
/** Get the total number of days since January 1, 1800 */
92
public static int getTotalNumberOfDays( int year, int month) {
getTotalNumberOfDays
93
int total = 0 ;
94
95 // Get the total days from 1800 to 1/1/year
96 for ( int i = 1800 ; i < year; i++)
97 if (isLeapYear(i))
98 total = total + 366 ;
99 else
100 total = total + 365 ;
101
102 // Add days from Jan to the month prior to the calendar month
103 for ( int i = 1 ; i < month; i++)
104 total = total + getNumberOfDaysInMonth(year, i);
105
106
return total;
107 }
108
109 /** Get the number of days in a month */
110 public static int getNumberOfDaysInMonth( int year, int month) {
111 if (month == 1 || month == 3 || month == 5 || month == 7 ||
112 month == 8 || month == 10 || month == 12 )
113
getNumberOfDaysInMonth
return 31 ;
114
115
if (month == 4 || month == 6 || month == 9 || month == 11 )
116
return 30 ;
117
118
if (month == 2 ) return isLeapYear(year) ? 29 : 28 ;
 
Search WWH ::




Custom Search