//----------------------------------日历表 2 3 4 import java.util.*; 5 import java.text.*; 6 7 class Menology 8 { 9 //----------------计算当前日期距离1900的天数 10 public long lastDays(String startDay,String seachDay){ 11 12 long lastDays = 0; 13 SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM"); 14 try{ 15 16 Date STD = SDF.parse(startDay); 17 Date SED = SDF.parse(seachDay); 18 lastDays = (SED.getTime() - STD.getTime()) / (24*60*60*1000); 19 }catch (ParseException pe){ 20 21 pe.printStackTrace(); 22 } 23 return lastDays; 24 } 25 26 //------------------确定每个月的天数 27 28 29 //--------------------------打印日历表的格式 30 public void MenologyTab(long week , int day){ 31 32 System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六"); 33 34 //-------日期天数计数 35 int number = 0; 36 for(int i = 0; i < 36; i++){ 37 38 if (i % 7 == 0 && i != 0){ 39 40 System.out.println(); 41 } 42 43 // 大于返回的日期后num计数,打印整月天数排版 44 if(i >= (int)week && i < day + (int)week){ 45 46 number++; 47 System.out.print(number + "\t"); 48 }else { 49 50 System.out.print("(>_<)\t"); 51 } 52 } 53 } 54 55 56 //计算每个月的天数 57 public int mounth(String mouth,String year){ 58 59 if(mouth.equals("1") || mouth.equals("3") || mouth.equals("5") || mouth.equals("7") || mouth.equals("8") || mouth.equals("10") || mouth.equals("12")){ 60 61 return 31; 62 }else if(mouth.equals("4") || mouth.equals("6") || mouth.equals("9") || mouth.equals("11")){ 63 64 return 30; 65 }else if(mouth.equals("2")){ 66 67 //------计算平年闰年 68 int years = Integer.parseInt(year); 69 if ((years % 4 == 0 && years % 100 != 0) || years % 400 == 0){ 70 71 return 29; 72 }else{ 73 74 return 28; 75 } 76 } 77 return 0; 78 } 79 } 80 81 class Dater 82 { 83 public static void main(String[] args) 84 { 85 //-----日历管理对象 86 Menology menology = new Menology(); 87 88 System.out.println("\n*******************************"); 89 System.out.println("**********日历表查询***********"); 90 System.out.println("*******************************\n"); 91 System.out.println("请输入你要查询的日期:"); 92 System.out.println("--------------------------------------------------------"); 93 Scanner keyInput = new Scanner(System.in); 94 String keyDate = keyInput.next(); 95 96 //-----取出年份 97 String year = keyDate.substring(0,4); 98 99 //------取出月份 100 String mouth = keyDate.substring(5);
menology.mounth(mouth,year); 102 103 //------计算天数 104 long Times = menology.lastDays("1900-1",keyDate); 105 System.out.println("--------------------------------------------------------"); 106 107 //-----传参进打印表格的方法 108 menology.MenologyTab(Times % 7 + 1,menology.mounth(mouth,year)); 109 } 11