Created
October 28, 2012 08:58
-
-
Save yao2030/3968100 to your computer and use it in GitHub Desktop.
calenday java implementating
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Calendar | |
{ | |
public static boolean isLeapYear(int year) | |
{ | |
if((year % 4 ==0 && year % 100 != 0) || (year % 400 == 0)) | |
return true; | |
return false; | |
} | |
public static String getMonth(int month) | |
{ | |
String[] m = {"", "January", "Febuary", "March", | |
"April", "May", "June", "July", | |
"August", "Setempter", "October", "November", "December"}; | |
return m[month]; | |
} | |
public static int getDays(int month, int year) | |
{ | |
int[] d = { 0 , 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; | |
if(isLeapYear(year)) | |
d[2] = 29; | |
return d[month]; | |
} | |
public static int getDay(int m, int d, int y) | |
{ | |
int y0 = y - (14-m)/12; | |
int x = y0 + y0/4 - y0/100 + y0/400; | |
int m0 = m + 12 * ((14-m)/12 - 2); | |
int d0 = (d + x + (31 * m0)/12) % 7; | |
return d0; | |
} | |
public static void showTitle(String[] days) | |
{ | |
for(int i = 0; i < days.length; i++) | |
StdOut.printf("%2s ", days[i]); | |
StdOut.println(); | |
} | |
public static void main(String[] args) | |
{ | |
int month = Integer.parseInt(args[0]); | |
int year = Integer.parseInt(args[1]); | |
String[] days = {" S", " M", "Tu", " W", "Th", " F", " S"}; | |
String m = getMonth(month); | |
StdOut.println(m + " " + year); | |
showTitle(days); | |
int day = getDay(month, 1, year); | |
int count = 0; | |
for(int i = 0; i < day; i++) | |
{ | |
StdOut.print(" "); | |
count++; | |
} | |
for(int i = 1; i <= getDays(month, year); i++) | |
{ | |
StdOut.printf("%2d ", i); | |
count++; | |
if(count % 7 == 0) | |
StdOut.println(); | |
} | |
StdOut.println(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment