Java简单模拟万年历

package com.demo.main;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		/*
		 * 任务需求1:按照如下要求,制作万年历,按每月31天算:                      
		 * 所需知识点:1.日期类
		 */
		
		Demo2();
	}

	private static void Demo2() {
		Scanner scr = new Scanner(System.in);
		System.out.println("请输入年份:");
		int yearNumber = scr.nextInt() ; 
		System.out.println("请输入月份:");
		int monthNumber = scr.nextInt();
		Calendar cd = Calendar.getInstance() ;
		//先设置对象中的属性参数(日期的值),月份是 0-11。
	    Demo1(yearNumber, monthNumber, cd);
	}

	private static void Demo1(int yearNumber, int monthNumber, Calendar cd) {
		cd.set(yearNumber,monthNumber-1,1);
		int day_of_the_week = cd.get(Calendar.DAY_OF_WEEK)-1;//本月第一天为星期几。
		int day_of_the_month = cd.getActualMaximum(Calendar.DAY_OF_MONTH);//本月中最大的天数为多少,也就是本月的天数是多少。
		GregorianCalendar gcd = new GregorianCalendar();
		boolean flag = gcd.isLeapYear(yearNumber);
		if(flag==true&&monthNumber==2){
			day_of_the_month = 29 ;
			Demo(day_of_the_week, day_of_the_month);
		}else{
			Demo(day_of_the_week, day_of_the_month);
		}
	}

	private static void Demo(int day_of_the_week, int day_of_the_month) {
		int[] day = new int[day_of_the_week+day_of_the_month];
		int index = 1 ;
		for(int i=day_of_the_week;i<day_of_the_week+day_of_the_month;i++){
			day[i] = index;
			index++;
		}
		System.out.println("周日\t周一\t周二\t周三\t周四\t周五\t周六");
		for(int i=0;i<day.length;i++){
			if((i+1)%7==0){
				System.out.println(day[i]);
			}else{
				System.out.print(day[i]+" \t");
			}
		}
	}

}

猜你喜欢

转载自blog.csdn.net/Ameir_yang/article/details/81661950