Yes! Busy every day, have forgotten that tomorrow is our happy weekend. Today, without the chat of friends, tomorrow is in a tense state! Another friend mentioned that all today to share with you to write a calendar table with Java, tired of want to rest when, open to see, still have a few days to the weekend! So let's look at the code, how to implement it!
public class Demo6 {
public static void Main (string[] args) {
 DEMO6 demo = new Demo6 ();
demo.show (2016,11) //enter year and month
}
//Define a method: Determine whether it is common year or leap year
public boolean isleapyear (int year) {
& nbsp if (year%400==0 | | (year%4==0 && year%100!=0)) {
return true;
}
return false;
//calculates the input year, the total number of days in the month
public int Gettotaldatefrom (int year, int month) {
int totaldate = 0 ;
for (int i = 1900; i < year; i++) {
Judge whether it is a leap year or common year
if (Isleapyear (year)) {
Totaldate + = 366;
}else{
Totaldate + = 365;
}
}
Calculate the number of days in February
for (int i = 1; i < month; i++) {
Totaldate + = GetDayOfMonth (year, month);
}
return totaldate;
}
Returns a number of days
public int getdayofmonth (int year,int month) {
Switch (month) {
Case 1:
Case 3:
Case 5:
Case 7:
Case 8:
Case 10:
Case 12:
return 31;
Case 4:
Case 9:
Case 11:
return 30;
Case 2:
if (Isleapyear (year)) {
return 29;
} else {
return 28;
}
default:
&NB Sp return 30;
}
}
public int printspace (int year, int month) {
&NBSP ; return Gettotaldatefrom (year, month)%7;
}
public void print (int year, int month) {
&N Bsp int countspac = Printspace (year, month) +1; //number of spaces
int coun = 0; //Define a Number
for (int i = 0; i < Countspac; i++) {
&NB Sp coun + +;
system.out.print ("\ t") );
}
Print Date
for (int i = 0; i < GetDayOfMonth (year, month); i++) {
if (coun% 7 = = 0) {
System.out.println ();
}
coun++;
System.out.print (i+ "\ t");
}
}
public void Show (int year, int month) {
System.out.println ("**************" +year+ "******" +month+ "***************");
System.out.println ("=======================================================");
System.out.println ("day \ t a \ Two \ three \ four \ t five \ t six");
System.out.println ("=======================================================");
Print (year, month);
System.out.println ();
}
}
The implementation results are as follows:
Use Java to make a calendar, want to take a look at the weekend and a few days away!