类图
代码部分
把额外的几个方法贴了上来
GetDay:
lastDay
1 //获得大月的前一天 2 public String getEvenYesterday(int m, int d, int y) { 3 boolean leapYear = isLeapYear(y); 4 String result = ""; 5 if(d < 1 || d > 31) { 6 result = "日期超出范围"; 7 } else { 8 if(m != 1 && m != 3){ 9 if(d == 1) {10 result = y + "年" + (m-1) + "月30日";11 } else {12 result = y + "年" + m + "月" + (d-1) + "日";13 }14 } else {15 switch(m) {16 case 1:17 result = (y-1) + "年12月31日";18 break;19 case 3:20 if(leapYear) {21 result = y + "年" + (m-1) + "月29日";22 } else {23 result = y + "年" + (m-1) + "月28日";24 }25 break;26 }27 }28 29 } 30 return result;31 }32 //获得小月的前一天33 public String getOddYesterday(int m, int d, int y) {34 String result = "";35 if(m != 2){36 if(d < 1 || d > 30) {37 result = "日期超出范围";38 } else { 39 if(d == 1) {40 result = y + "年" + (m-1) + "月31日";41 } else {42 result = y + "年" + m + "月" + (d-1) + "日";43 } 44 } 45 } else if (m == 2 && isLeapYear(y)){46 if(d < 1 || d > 29) {47 result = "日期超出范围";48 } else { 49 if(d == 1) {50 result = y + "年" + (m-1) + "月31日";51 } else {52 result = y + "年" + m + "月" + (d-1) + "日";53 } 54 } 55 } else {56 if(d < 1 || d > 28) {57 result = "日期超出范围";58 } else { 59 if(d == 1) {60 result = y + "年" + (m-1) + "月31日";61 } else {62 result = y + "年" + m + "月" + (d-1) + "日";63 } 64 } 65 }66 67 return result; 68 }69 //前一天判断70 public String lastDay(int m, int d, int y) {71 String result = "";72 if(y < 1912 || y > 2050) {73 result = "年份超出范围";74 } else {75 switch(m) {76 case 1:77 case 3:78 case 5:79 case 7:80 case 8:81 case 10:82 case 12:83 result = getEvenYesterday(m, d, y);84 break;85 case 2:86 case 4:87 case 6:88 case 9:89 case 11:90 result = getOddYesterday(m, d, y);91 break; 92 default:93 result = "月份超出范围";94 break;95 }96 }97 return result; 98 }
weekDay
1 //使用基姆拉尔森计算公式 2 //Week=(Day + 2*Month + 3*(Month+1)/5 + Year + Year/4 - Year/100 + Year/400) % 7 3 public int weekDay(int m, int d, int y) { 4 if(m == 1){ 5 m = 13; 6 y--; 7 } 8 if(m == 2){ 9 m = 14; 10 y--;11 }12 int week=(d + 2*m + 3*(m+1)/5 + y + y/4 - y/100 + y/400)%7;13 14 return week;15 }16 //将获得的数字转成输出显示的字符串17 public String todayWeek(int m, int d, int y) {18 int week = weekDay(m, d, y);19 String w = "";20 switch(week) {21 case 0:22 w = "一";break;23 case 1:24 w = "二";break;25 case 2:26 w = "三";break;27 case 3:28 w = "四";break;29 case 4:30 w = "五";break;31 case 5:32 w = "六";break;33 case 6:34 w = "日";break;35 } 36 return w;37 }
GraphicInterface:
这个类是用来绘制图形界面的,但是在事件处理中引用了GetDay中的方法,以便获得相应的信息,如前一天,后一天等
drawPicture
1 //绘制界面 2 public void drawPicture() { 3 JFrame frame = new JFrame("日期计算程序"); 4 frame.setLayout(null); 5 /*文字标签组*/ 6 JLabel title = new JLabel("请输入需要计算的年月日(1912-2050之间)"); 7 JLabel year = new JLabel("年:"); 8 JLabel month = new JLabel("月:"); 9 JLabel day = new JLabel("日:"); 10 JLabel thisWeek = new JLabel("这一天是星期"); 11 JLabel nextDay = new JLabel("下一天是:"); 12 JLabel prevDay = new JLabel("上一天是:"); 13 /*文字输入区域组*/ 14 JTextField t_year = new JTextField(); 15 JTextField t_month = new JTextField(); 16 JTextField t_day = new JTextField(); 17 JTextField t_thisWeek = new JTextField(); 18 JTextField t_nextDay = new JTextField(); 19 JTextField t_prevDay = new JTextField(); 20 /*按钮组*/ 21 JButton submit = new JButton("确定"); 22 JButton cancel = new JButton("清空"); 23 24 //使用匿名匿名内部类为submit按钮添加监听事件 25 submit.addActionListener(new ActionListener() { 26 @Override 27 public void actionPerformed(ActionEvent e) { 28 //判断触发源是否为submit按钮 29 if(e.getSource() == submit) { 30 //得到输入的年,月,日,返回的是String 字符串类型 31 String y = t_year.getText(); 32 String m = t_month.getText(); 33 String d = t_day.getText(); 34 //把获得的字符串转成数字 35 int i_y = Integer.parseInt(y); 36 int i_m = Integer.parseInt(m); 37 int i_d = Integer.parseInt(d); 38 //输出后一天 39 String nextD = gd.nextDate(i_m, i_d, i_y); 40 t_nextDay.setText(nextD); 41 //输出前一天 42 String prevD = gd.lastDay(i_m, i_d, i_y); 43 t_prevDay.setText(prevD); 44 //输入出当前是星期几 45 String week = gd.todayWeek(i_m, i_d, i_y); 46 t_thisWeek.setText(week); 47 } 48 49 } 50 }); 51 //使用匿名匿名内部类为cancel按钮添加监听事件 52 cancel.addActionListener(new ActionListener() { 53 @Override 54 public void actionPerformed(ActionEvent e) { 55 //清空输入的数据 56 if(e.getSource() == cancel) { 57 t_year.setText(""); 58 t_month.setText(""); 59 t_day.setText(""); 60 } 61 62 } 63 }); 64 65 /*设置标签位置及大小*/ 66 Font ft = new Font("黑体",Font.BOLD,24);//设置显示字体 67 title.setFont(ft); 68 title.setBounds(62, 62, 500, 25); 69 year.setFont(ft); 70 year.setBounds(80, 124, 50, 25); 71 month.setFont(ft); 72 month.setBounds(230, 124, 50, 25); 73 day.setFont(ft); 74 day.setBounds(380, 124, 50, 25); 75 thisWeek.setFont(ft); 76 thisWeek.setBounds(62, 248, 200, 25); 77 prevDay.setFont(ft); 78 prevDay.setBounds(62, 372, 150, 25); 79 nextDay.setFont(ft); 80 nextDay.setBounds(62, 310, 150, 25); 81 /*设置文字位置及大小*/ 82 t_year.setBounds(130, 124, 50, 25); 83 t_month.setBounds(280, 124, 50, 25); 84 t_day.setBounds(430, 124, 50, 25); 85 t_thisWeek.setBounds(230, 248, 50, 25); 86 t_thisWeek.setFont(ft); 87 t_thisWeek.setEnabled(false);//设置为无法输入 88 t_nextDay.setBounds(180, 310, 200, 25); 89 t_nextDay.setFont(ft); 90 t_nextDay.setEnabled(false);//设置为无法输入 91 t_prevDay.setBounds(180, 372, 200, 25); 92 t_prevDay.setFont(ft); 93 t_prevDay.setEnabled(false);//设置为无法输入 94 /*设置按钮位置及大小*/ 95 submit.setBounds(180, 187, 62, 31); 96 cancel.setBounds(320, 187, 62, 31); 97 //把所有组件添加到frame中 98 frame.add(title);frame.add(year);frame.add(month);frame.add(day); 99 frame.add(thisWeek);frame.add(nextDay);frame.add(prevDay);100 frame.add(t_year);frame.add(t_month);frame.add(t_day);101 frame.add(t_thisWeek);frame.add(t_nextDay);frame.add(t_prevDay);102 frame.add(submit);frame.add(cancel);103 /*设置显示画布大小及显示的位置*/104 frame.setSize(600, 490);105 frame.setLocation(300,300);106 frame.setVisible(true);107 108 }
结果: