JAVA 输入年份写万年历

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六");
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值