java 日历查询小笔记

刚接触java写的日历查询笔记,接触了2天java,我觉得我喜欢偷笑

//直接输入年份月份展示出对应的日历
import java.util.*;
public class calendar6 {
	public static void main(String[] args) {
		//定义字符
		String flag;
		//输入判断是否再次执行
		do {
			Scanner inScanner = new Scanner(System.in);
			System.out.println("请输入你要查询的年份: ");
			int year = inScanner.nextInt();
			System.out.println("请输入你要查询的月份: ");
			int month = inScanner.nextInt();
			//定义改月份的天数
			int day;
			//1.瑞年 366 2.平年 365
			int sum = 0;
			for (int i = 1900; i < year; i++) {
				//先判断是瑞年还是平年
				if (year%4==0&&year%100!=0||year%400==0) {
					sum += 366;
				}else{
					sum += 365;
				}
			}
			//这里说明如果是瑞年的话2月是29天,如果是平年的话2月是28天
			for (int i = 1; i < month; i++) {
				if (i==2) {
					if (year%4==0&&year%100!=0||year%400==0) {	
						sum += 29;
					}else{
						sum += 28;
					}
				}else{
					if (i==4||i==6||i==9||i==11) {
						sum += 30;
					}else{
						sum += 31;
					}
					
				}
				
			}
			//这里依旧要给求的值加1,假如输入的2014年6份的话,求的是1900年1月1号到2014年5月31号的天数,现在求的是是2014年6月份的日历肯定是从2014年6月1号开始的
			//要是算天数的话相当于求的是1900年1月1号到2014年6月1号的天数,之前求的是1990年1月1号到2014年6月份的天数,所以要加上1
			sum += 1;
			int wekday = sum%7;
			System.out.println("日\t一\t二\t三\t四\t五\t六");
			for (int i = 1; i <= wekday; i++) {
				System.out.print("\t");
			}
			//这里要再加判断,该月份是多少天,先判断是瑞年还是平年
			if (month==1||month==3||month==5||month==7||month==8||month==10||month==12){
				day = 31;
			}else if (month==4||month==6||month==9||month==11) {
				day = 30;
			}else{
				if (year%4==0&&year%100!=0||year%400==0){
					day = 29;
				}else{
					day = 28;
				}
			}
			
			for (int i = 1; i <= day ; i++) {
				if (sum%7==6) {
					System.out.print(i+"\n");
				}else{
					System.out.print(i+"\t");
				}
				//循环的是1号号的2号的话这里sum++
				sum++;
			}
			System.out.println();
			System.out.println();
			System.out.println("还要继续查询码?(y/n)");
			flag = inScanner.next();
		} while (flag.equals("y"));
	}
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值