首页 > 其他分享 >获取某年某月

获取某年某月

时间:2022-09-21 16:11:53浏览次数:75  
标签:int sum System 获取 static 某年某月 println out


/**
 * @模块名:yefeng
 * @类名称:Renqi
 * @类描述:日期升级版
 * @版本:  1.0
 * @创建人:叶枫
 * @创建时间:2020年2月3日下午8:51:32
 */

public class Renqi {
	public static int year = Integer.MIN_VALUE;
	public static int month = Integer.MIN_VALUE;
	private static int[] dayOfmonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

	public static void main(String[] args) {
		PrintCalt();
	}
	public static void PrintCalt() {
		//1.让用户输入年份和月份
		InputYearANDMonth();
		//2.计算1900-1-1 到用户输入的年份月份的总天数
		//2-1计算各年的天数
		int sum = getsumDayOfYear();
		//2-2计算各月的天数
		sum += getSumDayMonth();
		System.out.println(sum % 7);
		sum++;
		//3.月份标题
		PrintMonth();
		//5.根据某年1日和星期几,打印月历的内容
		PrintDay(sum % 7);
	}
	private static void PrintDay(int daywenkt) {
		int  cont = daywenkt -1;
		if(daywenkt == 0) {
			cont = 6;
		}
		else {
			cont = daywenkt -1;
		}
		for(int i = 0 ; i < cont; i++) {
			System.out.print("\t");
		}
		for(int i = 0 ; i < dayOfmonth[month -1];i++) {
			System.out.print(i + 1);
			if((daywenkt + i) % 7 ==0) {
				System.out.println();
			}
			else {
				System.out.print("\t");
			}
		}
		if(isYear(year) && month == 2) {
			System.out.println(29);
		}
	}

	private static void PrintMonth() {
		String[] monthName = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
		System.out.println(year + "\t" + monthName[month -1]);
		String[] weeksday = {"星期一","星期二 "," 星期三 "," 星期四 "," 星期五"," 星期六"," 星期天"};
		for (int i = 0; i < weeksday.length; i++) {
			System.out.print(weeksday[i]+"\t");
		}
		System.out.println();
	}
	/**
	 * 	 输入年份和月份
	 */
	public  static void InputYearANDMonth() {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入年份:");
		year = input.nextInt();
		System.out.println("请输入月份:");
		month = input.nextInt();
		input.close();
		input = null;
	}
	/*
	 *	判断是否输入年份和月份 并加上年份
	 */

	private static int getsumDayOfYear() {
		if(year <= Integer.MIN_VALUE) {
			System.out.println("年份错误!请重新输入年份和月份!");
			InputYearANDMonth();
		}
		int sum = 0;
		for (int i = 1900; i < year; i++) {
			sum += 365;//每一年加上365
			if(isYear(i)) {
				sum++;
			}
		}
		return sum;
	}
	//判断是不是闰年
	private static boolean isYear(int year) {
		return year % 400 ==0 || year % 4 == 0 && year % 100 != 0;
	}
	/**
	 *	 计算月份
	 */
	private static int getSumDayMonth() {
		int sum = 0;
		for(int i = 0 ; i < month - 1; i++) {
			sum += dayOfmonth[i];
		}
		if(isYear(year) && month >= 3) {
			sum++;
		}
		return sum;
	}
}

标签:int,sum,System,获取,static,某年某月,println,out
From: https://www.cnblogs.com/lengyingmofeng/p/16715935.html

相关文章