核心为将日期日历装入一个二维数组,从而能方便的对日历天数进行调用开发,目前第一版实现四种功能,后续还能进行一些其他的玩法研究,同时涉及到用户输入时候的数据不规范也定义了一个异常类用来处理输入异常。
- package cn.mxy.demo;
- import java.util.*;
- class PrintCalendar {
- int year;
- int month;
- int function;
- int[][] days;
- Calendar cal = Calendar.getInstance();
- Scanner input = new Scanner(System.in);
- public void printCalendar(){
- switchFunction();
- print(this.function);
- }
-
- public void print(int function){
- switch(function){
- case 1:{
- printMonthTitle(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1);
- printDays(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1);
- } break;
- case 2:{
- for(int i = 1; i < 13; i ++){
- printMonthTitle(cal.get(Calendar.YEAR), i);
- printDays(cal.get(Calendar.YEAR), i);
- }
- } break;
- case 3:{
- boolean yearInput = true;
- boolean monthInput = true;
- do{
- try{
- System.out.println("请输入要查询的年份:");
- String str = input.nextLine();
- YearAndMonthException.checkInt(str);
- this.year = Integer.parseInt(str.replaceAll(" ",""));
- YearAndMonthException.checkYear(this.year);
- yearInput = false;
- }catch(YearAndMonthException yamy){
- System.out.println("发生错误:n"+yamy.getMessage() + "r");
- }
- }while(yearInput);
- do{
- try{
- System.out.println("请输入要查询的月份:");
- String str = input.nextLine();
- YearAndMonthException.checkInt(str);
- this.month = Integer.parseInt(str.replaceAll(" ",""));
- YearAndMonthException.checkMonth(this.month);
- monthInput = false;
- }catch(YearAndMonthException yamm){
- System.out.println("发生错误:n"+yamm.getMessage() + "r");
- }
- }while(monthInput);
- printMonthTitle(this.year, this.month);
- printDays(this.year, this.month);
- } break;
- case 4:{
- boolean yearInput = true;
- do{
- try{
- System.out.println("请输入要查询的年份:");
- String str = input.nextLine();
- YearAndMonthException.checkInt(str);
- this.year = Integer.parseInt(str.replaceAll(" ",""));
- YearAndMonthException.checkYear(this.year);
- yearInput = false;
- }catch(YearAndMonthException yamy){
- System.out.println("发生错误:n"+yamy.getMessage() + "r");
- }
- }while(yearInput);
- for(int i = 1; i < 13; i ++){
- printMonthTitle(year, i);
- printDays(year, i);
- }
- } break;
- }
- }
-
- public void switchFunction(){
- System.out.println("请选择要查看的内容:"+"n"+
- "1: 查看当月日历"+"n"+
- "2: 查看当年全年日历"+"n"+
- "3: 查看指定年月日历"+"n"+
- "4: 查看指定年全年日历"+"n"
- );
- boolean functionInput = true;
- do{
- try{
- String str = input.nextLine();
- YearAndMonthException.checkInt(str);
- this.function = Integer.parseInt(str.replaceAll(" ",""));
- YearAndMonthException.checkFunction(this.function);
- functionInput = false;
- }catch(YearAndMonthException yamf){
- System.out.println("发生错误:n"+yamf.getMessage() + "r");
- }
- }while(functionInput);
- }
-
- public void printMonthTitle(int year, int month){ //打印日历头.printMonthTitle()方法的具体实现过程。
- String chYear = String.valueOf(year)+"年";
- String chMonth = null;
- if (month <= 10 && month > 0)
- switch(month){
- case 1: chMonth = " 一 月份";
- break;
- case 2: chMonth = " 二 月份";
- break;
- case 3: chMonth = " 三 月份";
- break;
- case 4: chMonth = " 四 月份";
- break;
- case 5: chMonth = " 五 月份";
- break;
- case 6: chMonth = " 六 月份";
- break;
- case 7: chMonth = " 七 月份";
- break;
- case 8: chMonth = " 八 月份";
- break;
- case 9: chMonth = " 九 月份";
- break;
- case 10: chMonth = " 十 月份";
- break;
- }
- else if(month > 10 && month < 13){
- switch(month){
- case 11: chMonth = "十一 月份";
- break;
- case 12: chMonth = "十二 月份";
- break;
- }
- }
- else{}
- System.out.println(" "+chYear+" "+chMonth);
- System.out.println("--------星期--------");
- System.out.println("一 二 三 四 五 六 天");
- }
-
- public void printDays(int year, int month){ //打印出日历天数组:通过.getDays()方法获得日期排列的二维int类型数组,再通过.strDays()方法转化为String类型进行打印。
- String[][] strDays;
- strDays = strDays(getDays(year, month));
- for(int a = 0; a < strDays.length; a++){
- for(int b = 0; b < strDays[a].length; b++){
- System.out.print(strDays[a][b]+" ");
- }
- System.out.println();
- }
- System.out.println();
- }
-
- public int[][] getDays(int year, int month){ //获得当月的int类型日历数组,需要通过.mSum()方法获得当月总天数,通过.weekNum()方法获得当月第一天是周几,再进行数组获取
- int i = 0;
- int mSum = mSum(year, month);
- int weekNum = weekNum(year, month);
- if(!isLeapYear(year) && month == 2 && weekNum == 1)
- i = 4;
- else if(weekNum == 5 ){
- switch(month){
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12: i = 6;
- break;
- case 2:
- case 4:
- case 6:
- case 9:
- case 11: i = 5;
- break;
- }
- }
- else if(weekNum > 5 && month != 2){
- i = 6;
- }
- else
- i = 5;
- int[][] days = new int[i][7];
- if(weekNum != 1){
- int sum = 1;
- for(int b0 = 0; b0 < weekNum-1; b0++){
- days[0][b0] = 0;
- }
- for(int b1 = weekNum-1; b1 < 7; b1++){
- days[0][b1] = sum;
- sum++;
- }
- int sum1 = sum + 1;
- for(int a = 1;a < i; a++){
- for(int b3 = 0; b3 < 7; b3++){
- if(sum <= mSum){
- days[a][b3] = sum;
- sum++;
- }
- else
- break;
- }
- }
- }
- else{
- int sum1 = 1;
- for(int a = 0;a < i; a++){
- for(int b4 = 0; b4 < 7; b4++){
- if(sum1 <= mSum){
- days[a][b4] = sum1;
- sum1++;
- }
- else
- break;
- }
- }
- }
- return days;
- }
- public String[][] strDays(int days[][]){ //将int类型二维日期数组转换为String类型的方法
- String[][] strDays = new String[days.length][7];
- for(int a = 0; a < days.length; a++){
- for(int b = 0; b < 7; b++){
- if(days[a][b] == 0)
- strDays[a][b] = " ";
- else if(days[a][b] > 0 && days[a][b] < 10)
- strDays[a][b] = String.valueOf(" "+days[a][b]);
- else
- strDays[a][b] = String.valueOf(days[a][b]);
- }
- }
- return strDays;
- }
- public boolean isLeapYear(int year){ //根据所给年份计算当年是不是闰年
- if (year % 4 == 0 && year % 100!=0 || year%400==0)
- return true;
- else
- return false;
- }
-
- public int mSum(int year, int month){ //根据所给年份及月份计算当月共有几天
- int r = 0;
- switch(month){
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- r = 31;
- break;
- case 4:
- case 6:
- case 9:
- case 11:
- r = 30;
- break;
- case 2:
- if(isLeapYear(year))
- r = 29;
- else
- r = 28;
- break;
- }
- return r;
- }
- public int pastDays(int year, int month){ //获得当年到所求月份之前的天数
- int p = 0;
- if(isLeapYear(year)){
- switch(month){
- case 1: p = 0; break;
- case 2: p = 31; break;
- case 3: p = 60; break;
- case 4: p = 91; break;
- case 5: p = 121; break;
- case 6: p = 152; break;
- case 7: p = 182; break;
- case 8: p = 213; break;
- case 9: p = 244; break;
- case 10: p = 274; break;
- case 11: p = 305; break;
- case 12:p = 335; break;
- }
- }
- else{
- switch(month){
- case 1: p = 0; break;
- case 2: p = 31; break;
- case 3: p = 59; break;
- case 4: p = 90; break;
- case 5: p = 120; break;
- case 6: p = 151; break;
- case 7: p = 181; break;
- case 8: p = 212; break;
- case 9: p = 243; break;
- case 10: p = 273; break;
- case 11: p = 304; break;
- case 12:p = 334; break;
- }
- }
- return p;
- }
-
- public int weekNum(int year, int month){ //计算所求月份当月第一天是星期几
- int p = pastDays(year, month);
- int l = 0;
- int w = 0;
- int z;
- for(int s = 1800; s < year; s++){
- if(isLeapYear(s))
- l++;
- else
- continue;
- }
- z = 366*l + 365*(year-1800-l)+p;
- w = (z + 3)%7;
- if(w == 0)
- w = 7;
- return w;
- }
- }
复制代码
- package cn.mxy.demo;
- public class YearAndMonthException extends Exception //为PrintCalendar类所涉及到的输入异常做申明
- {
- static String str;
-
- public String getMessage(){
- return str;
- }
-
- public static void checkInt(String str2) throws YearAndMonthException{
- String str1 = str2.replaceAll(" ", "");
- char[] ch = str1.toCharArray();
- for(int i = 0; i < ch.length; i ++){
- if(!Character.isDigit(ch [i])){
- str = "输入内容不能含有非数字成分,请重新输入:";
- throw new YearAndMonthException();
- }
- else{}
- }
- }
- public static void checkFunction(int fun) throws YearAndMonthException{
- if(fun < 1 || fun > 4){
- str = "功能选择错误(按指定编号选择),请重新输入:";
- throw new YearAndMonthException();
- }
- else{}
- }
- public static void checkYear(int year) throws YearAndMonthException{
- if(year < 1800){
- str = "年份输入错误(1800年以后),请重新输入:";
- throw new YearAndMonthException();
- }
- else{}
- }
- public static void checkMonth(int month) throws YearAndMonthException{
- if(month < 1 || month > 12){
- str = "月份输入错误(1月到12月之间),请重新输入:";
- throw new YearAndMonthException();
- }
- else{}
- }
- }
复制代码
- package cn.mxy.demo;
- public class Test {
- public static void main(String args[]){
- PrintCalendar pt = new PrintCalendar();
- pt.printCalendar();
- }
- }
复制代码 |