很实用的一个小程序(日历)

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)

System.out.println();

}
}

效果图
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

让程序飞

您的鼓励将是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值