Calendar
- 格式:doc
- 大小:47.50 KB
- 文档页数:6
package com;public class Calendar {private static final int INIYEAR = 1900;private static final int LEAPDAYS = 366;private static final int NONLEAPDAYS = 365;private int thisYear;private static Calendar calendar;static {calendar = new Calendar();}public static Calendar getInstance() {return calendar;}public static void main(String[] args) {// Calendar.getInstance().monthCalendar(2012, 6);Calendar.getInstance().yearAllCalendar(2012);}public void yearAllCalendar(int year){for(int i=1;i<=12;i++){monthCalendar(year, i);}}public void monthCalendar(int year, int month) {System.out.println("\n\n------------------------" + year + "年" + month+ "月" + "-----------------------");System.out.println("日\t一\t二\t三\t四\t五\t六");int thisMonthDays = oneMonthDays(month);int fristDay = (monthOfWeek(year, month) + 1) % 7 - 1;for (int i = 0; i < fristDay; i++) {System.out.print("\t");}for (int i = 1; i <= thisMonthDays; i++) {System.out.print(i + "\t");if ((fristDay + i) % 7 == 0) {System.out.println();}}System.out.println("\n-----------------------<<<<<<>>>>>---------------------");}public static boolean isLeapYear(int year) {if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { return true;}return false;}public static int totalDays(int year) {int years = year - INIYEAR;int toYear = INIYEAR;int totalDays = 0;if (years - toYear > 0) {return -1;}while (toYear < year) {if (isLeapYear(toYear)) {totalDays += LEAPDAYS;} else {totalDays += NONLEAPDAYS;}toYear++;}return totalDays;}public int dayOfWeek(int days) {return days % 7;}public static int oneMonthDays(int month) {switch (month) {case 4:case 6:case 9:case 11:return 30;case 2:if (isLeapYear(Calendar.getInstance().getThisYear())) { return 29;} else {return 28;}default:return 31;}}public int monthsDays(int month) {int monthsDays = 0;for (int i = 1; i < month; i++) {monthsDays += oneMonthDays(i);}return monthsDays;}public int yearAndMonthDays(int year, int month) {return totalDays(year) + monthsDays(month);}private int monthOfWeek(int year, int month) {returnCalendar.getInstance().dayOfWeek(Calendar.getInstance().yearAndMonthD ays(year, month)) + 1;}public void setThisYear(int thisYear) {this.thisYear = thisYear;}public int getThisYear() {return thisYear;}}/*输出结果:------------------------2012年1月-----------------------日一二三四五六1 2 3 4 5 6 78 9 10 11 12 13 1415 16 17 18 19 20 2122 23 24 25 26 27 2829 30 31-----------------------<<<<<<>>>>>---------------------------------------------2012年2月----------------------- 日一二三四五六1 2 3 45 6 7 8 9 10 1112 13 14 15 16 17 1819 20 21 22 23 24 2526 27 28 29-----------------------<<<<<<>>>>>---------------------------------------------2012年3月----------------------- 日一二三四五六1 2 34 5 6 7 8 9 1011 12 13 14 15 16 1718 19 20 21 22 23 2425 26 27 28 29 30 31-----------------------<<<<<<>>>>>---------------------------------------------2012年4月----------------------- 日一二三四五六1 2 3 4 5 6 78 9 10 11 12 13 1415 16 17 18 19 20 2122 23 24 25 26 27 2829 30-----------------------<<<<<<>>>>>---------------------------------------------2012年5月----------------------- 日一二三四五六1 2 3 4 56 7 8 9 10 11 1213 14 15 16 17 18 1920 21 22 23 24 25 2627 28 29 30 31-----------------------<<<<<<>>>>>---------------------------------------------2012年6月----------------------- 日一二三四五六1 23 4 5 6 7 8 910 11 12 13 14 15 1617 18 19 20 21 22 2324 25 26 27 28 29 30-----------------------<<<<<<>>>>>---------------------------------------------2012年7月----------------------- 日一二三四五六1 2 3 4 5 6 78 9 10 11 12 13 1415 16 17 18 19 20 2122 23 24 25 26 27 2829 30 31-----------------------<<<<<<>>>>>---------------------------------------------2012年8月----------------------- 日一二三四五六1 2 3 45 6 7 8 9 10 1112 13 14 15 16 17 1819 20 21 22 23 24 2526 27 28 29 30 31-----------------------<<<<<<>>>>>---------------------------------------------2012年9月----------------------- 日一二三四五六12 3 4 5 6 7 89 10 11 12 13 14 1516 17 18 19 20 21 2223 24 25 26 27 28 2930-----------------------<<<<<<>>>>>--------------------- ------------------------2012年10月-----------------------日一二三四五六1 2 3 4 5 67 8 9 10 11 12 1314 15 16 17 18 19 2021 22 23 24 25 26 2728 29 30 31-----------------------<<<<<<>>>>>---------------------------------------------2012年11月----------------------- 日一二三四五六1 2 34 5 6 7 8 9 1011 12 13 14 15 16 1718 19 20 21 22 23 2425 26 27 28 29 30-----------------------<<<<<<>>>>>---------------------------------------------2012年12月----------------------- 日一二三四五六12 3 4 5 6 7 89 10 11 12 13 14 1516 17 18 19 20 21 2223 24 25 26 27 28 2930 31-----------------------<<<<<<>>>>>--------------------- */。