package java320;
import java.util.Scanner;
public class P3 {
public static void main(String[] args) {
Scanner input =new Scanner(System.in); 定义键盘输入缓存区
System.out.print("请输入年份:");
int s=input.nextInt();
System.out.print("请输入月份:");
int s1=input.nextInt();
System.out.println("\t\t "+s+"年"+s1+"月");
cal(s,s1);
}
/**
* 判断指定年是否闰年
* @param year 年份
* @return
*/
public static boolean isr(int year){ 判断闰年
return (year%400==0) ||(year%100!=0 && year%4==0);
}
/**
* 求指定年的天数
* @param year 年份
* @return
*/
public static int daysInYear(int year){ 得到天数
if(isr(year)){
return 366;
}else{
return 365;
}
}
/**
* 指定年份和月份,获取该月份的天数
* @param year
* @param month
* @return
*/
public static int daysInMonth(int year,int month){
int r=31;
switch(month){ 每个月的天数
case 2:
if(isr(year)){
r=29; 平年和闰年2月份所代表的天数
}else{
r=28;
}
break;
case 4:
case 6:
case 9:
case 11:
r=30;
break;
}
return r;
}
/**
* 获取指定年月份的第一天是星期几
* @param year
* @param month
* @return 1-7 星期日--星期六
*/
public static int getFirstWeekDayOfMonth(int year,int month){
以1900 1.1为例得以下公式
int days=0;
//到前一年年底一共多少天
for(int y=1900;y<year;y++){
days+=daysInYear(y);
}
//今年的前一个月底一共多少天
for(int m=1;m<month;m++){
days+=daysInMonth(year,m);
}
days+=1;
//2017 3
int w=days%7;
return w+1;
}
/**
* 打印日历
* @param year
* @param month
*/
public static void cal(int year,int month){
System.out.println("日\t一\t二\t三\t四\t五\t六");
int w=getFirstWeekDayOfMonth(year,month);
int cnt=0;
for(int i=1;i<w;i++){
System.out.print("\t");
cnt++;
}
for(int i=1;i<=daysInMonth(year,month);i++){
System.out.print(i);
System.out.print("\t");
cnt++;
if(cnt%7==0)
}
效果图
}
import java.util.Scanner;
public class P3 {
public static void main(String[] args) {
Scanner input =new Scanner(System.in); 定义键盘输入缓存区
System.out.print("请输入年份:");
int s=input.nextInt();
System.out.print("请输入月份:");
int s1=input.nextInt();
System.out.println("\t\t "+s+"年"+s1+"月");
cal(s,s1);
}
/**
* 判断指定年是否闰年
* @param year 年份
* @return
*/
public static boolean isr(int year){ 判断闰年
return (year%400==0) ||(year%100!=0 && year%4==0);
}
/**
* 求指定年的天数
* @param year 年份
* @return
*/
public static int daysInYear(int year){ 得到天数
if(isr(year)){
return 366;
}else{
return 365;
}
}
/**
* 指定年份和月份,获取该月份的天数
* @param year
* @param month
* @return
*/
public static int daysInMonth(int year,int month){
int r=31;
switch(month){ 每个月的天数
case 2:
if(isr(year)){
r=29; 平年和闰年2月份所代表的天数
}else{
r=28;
}
break;
case 4:
case 6:
case 9:
case 11:
r=30;
break;
}
return r;
}
/**
* 获取指定年月份的第一天是星期几
* @param year
* @param month
* @return 1-7 星期日--星期六
*/
public static int getFirstWeekDayOfMonth(int year,int month){
以1900 1.1为例得以下公式
int days=0;
//到前一年年底一共多少天
for(int y=1900;y<year;y++){
days+=daysInYear(y);
}
//今年的前一个月底一共多少天
for(int m=1;m<month;m++){
days+=daysInMonth(year,m);
}
days+=1;
//2017 3
int w=days%7;
return w+1;
}
/**
* 打印日历
* @param year
* @param month
*/
public static void cal(int year,int month){
System.out.println("日\t一\t二\t三\t四\t五\t六");
int w=getFirstWeekDayOfMonth(year,month);
int cnt=0;
for(int i=1;i<w;i++){
System.out.print("\t");
cnt++;
}
for(int i=1;i<=daysInMonth(year,month);i++){
System.out.print(i);
System.out.print("\t");
cnt++;
if(cnt%7==0)
System.out.println();
}
}
效果图
}