import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class Test {
public static void main(String[] args) {
GregorianCalendar currentDay = new GregorianCalendar();//这个能够获得系统的时间
int year= currentDay.get(Calendar.YEAR);//随便放置个年份
printMonth(2012);
}
//获取当月的天数目
public static int getMonthDay(int month){
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
if(isLeapYear(month)){
return 29;
}else{
return 28;
}
default:
return 0;
}
}
//判断是否是闰年
public static Boolean isLeapYear(int year){
return (year % 400 == 0) || (year % 4 == 0) && (year % 100 != 0);
}
//打印每月日历
public static void printMonth(int year,int... month){
for(int m=1;m<13;m++){
int cols = firstDayNum(year,m)-1;
printWeek(m);
for(int i=1; i <=cols; i++ ){
System.out.print("\t");
}
for(int i=1; i <= getMonthDay(5); i++ ){
System.out.print("\t"+i);
cols++;
if(cols>=7){
System.out.println("");
cols=0;
}
}
System.out.println("");
System.out.println("");
}
}
//一个月的第一天是星期几,一月month=
public static int firstDayNum(int year,int month){
int mth = month-1;
Date temp = new Date(year,mth,1);
//temp.setDate('0');
return temp.getDay();
}
//日历表头输出
public static void printWeek(int m){
System.out.println("**********************************"+m+"月份**********************************");
System.out.println("\t日 "+"\t一"+"\t二"+"\t三"+"\t四 "+"\t五"+"\t六");
}
}
import java.util.Date;
import java.util.GregorianCalendar;
public class Test {
public static void main(String[] args) {
GregorianCalendar currentDay = new GregorianCalendar();//这个能够获得系统的时间
int year= currentDay.get(Calendar.YEAR);//随便放置个年份
printMonth(2012);
}
//获取当月的天数目
public static int getMonthDay(int month){
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
if(isLeapYear(month)){
return 29;
}else{
return 28;
}
default:
return 0;
}
}
//判断是否是闰年
public static Boolean isLeapYear(int year){
return (year % 400 == 0) || (year % 4 == 0) && (year % 100 != 0);
}
//打印每月日历
public static void printMonth(int year,int... month){
for(int m=1;m<13;m++){
int cols = firstDayNum(year,m)-1;
printWeek(m);
for(int i=1; i <=cols; i++ ){
System.out.print("\t");
}
for(int i=1; i <= getMonthDay(5); i++ ){
System.out.print("\t"+i);
cols++;
if(cols>=7){
System.out.println("");
cols=0;
}
}
System.out.println("");
System.out.println("");
}
}
//一个月的第一天是星期几,一月month=
public static int firstDayNum(int year,int month){
int mth = month-1;
Date temp = new Date(year,mth,1);
//temp.setDate('0');
return temp.getDay();
}
//日历表头输出
public static void printWeek(int m){
System.out.println("**********************************"+m+"月份**********************************");
System.out.println("\t日 "+"\t一"+"\t二"+"\t三"+"\t四 "+"\t五"+"\t六");
}
}