- package com.test2;
- import java.util.Scanner;
- public class Demo2 {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- System.out.println("请输入年份:");
- Scanner sc = new Scanner(System.in);
- int years = sc.nextInt();
- Year y = new Year();
- y.weekDay = y.firstWeekDay(years);
- y.year = years;
- System.out.println("\n "+years+"年 ");
- y.showMonth();
- }
- }
- class Year {
- public static int year, weekDay;
- public boolean isLeapYear(int year) {
- return ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));
- }
- public int firstWeekDay(int year) {
- long day = year * 365;
- for (int i = 1; i < year; i++) {
- if (isLeapYear(i)) {
- day += 1;
- }
- }
- return (int) (day % 7);
- }
- public int getMonthDay(int month) {
- switch (month) {
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- return 31;
- case 4:
- case 6:
- case 9:
- case 11:
- return 30;
- case 2:
- if (isLeapYear(year)) {
- return 29;
- } else {
- return 28;
- }
- default:
- return 0;
- }
- }
- public void showMonth() {
- for (int m = 1; m <= 12; m++) {
- System.out.println(m + "月");
- System.out.println(" Sunday Monday Tuesday Wednesday Thursday Friday Saturday");
- for (int j = 1; j <= weekDay; j++) {
- System.out.print(" ");
- }
- int monthDay = getMonthDay(m);
- for (int d = 1; d <= monthDay; d++) {
- if (d < 10) {
- System.out.print(" " + "0" + d + " ");
- } else {
- System.out.print(" " + d + " ");
- }
- weekDay = (weekDay + 1) % 7;
- if (weekDay == 0) {
- System.out.println();
- }
- }
- System.out.println();
- }
- }
- }
转载于:https://blog.51cto.com/skylight919/882302