第4次作业类测试代码+105032014161+张丽霞
1、类图:
2、界面与功能:
1)正常判断上一天,下一天,星期几:
2)判断不合法日期:
3)当年份,月份或日期不在范围内时,提示错误:
4)当输入不合规范时,提示“输入有误请重新输入”:
5)点击cancel,重置所有内容:
3、代码:
Date.java类:
1 import java.text.ParseException; 2 import java.text.SimpleDateFormat; 3 import java.util.Calendar; 4 public class Date { 5 6 //next day方法 7 public String NextDay(int year,int month,int day){ 8 //判断大月且不是12月31号 9 if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 ||( month == 12 && day<31)) 10 { 11 if (day < 31) 12 day++; 13 else if (day == 31) 14 { 15 month++; 16 day =1; 17 } 18 } 19 //判断12月31号 20 else if (month == 12 && day == 31) 21 { 22 year++; 23 month = 1; 24 day = 1; 25 } 26 //判断除了2月的小月 27 else if (month == 4 || month == 6 || month == 9 || month == 11) 28 { 29 if (day < 30) 30 day++; 31 else if (day == 30) 32 { 33 month++; 34 day = 1; 35 } 36 } 37 //2月需判断是否闰年 38 else if (month == 2) 39 { 40 if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){ 41 if (day < 29) 42 day++; 43 else if (day == 29) 44 { 45 month++; 46 day = 1; 47 } 48 } 49 else{ 50 if (day < 28) 51 day++; 52 else if (day == 28) 53 { 54 month++; 55 day = 1; 56 } 57 } 58 } 59 return year+"年"+month+"月"+day+"日"; 60 } 61 62 63 public String lastDay(int year,int month,int day){ 64 //判断除了3月的大月且不是1月1号 65 if ((month == 1 && day>1) || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) 66 { 67 if (day == 1) 68 { 69 month--; 70 day =30; 71 }else 72 day--; 73 } 74 //判断1月1号 75 else if (month == 1 && day == 1) 76 { 77 year--; 78 month = 12; 79 day = 31; 80 } 81 //判断除了2月的小月 82 else if (month==2|| month == 4 || month == 6 || month == 9 || month == 11) 83 { 84 if (day == 1) 85 { 86 month--; 87 day = 31; 88 }else 89 day--; 90 } 91 //当为3月时,2月需判断是否闰年 92 else if (month == 3) 93 { 94 if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){ 95 if (day == 1) 96 { 97 month--; 98 day = 29; 99 } 100 else { 101 day--; 102 } 103 } 104 else{ 105 if (day == 1) 106 { 107 month--; 108 day = 28; 109 } 110 else 111 day--; 112 } 113 } 114 return year+"年"+month+"月"+day+"日"; 115 116 } 117 //要求返回int... 118 public int weekDay( int year,int month,int day) throws ParseException{ 119 String pTime=year+"-"+month+"-"+day; 120 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 121 Calendar c = Calendar.getInstance(); 122 c.setTime(format.parse(pTime)); 123 124 int dayForWeek = 0; 125 if(c.get(Calendar.DAY_OF_WEEK) == 1){ 126 dayForWeek = 7; 127 }else{ 128 dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1; 129 } 130 return dayForWeek; 131 } 132 //判断日期是否合法 133 public boolean isValidDate(String inDate) { 134 135 if (inDate == null) 136 return false; 137 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 138 139 // if (inDate.trim().length() != dateFormat.toPattern().length()) 140 // return false; 141 142 dateFormat.setLenient(false); 143 144 try { 145 dateFormat.parse(inDate.trim()); 146 } 147 catch (ParseException pe) { 148 return false; 149 } 150 return true; 151 } 152 //转化为汉字 153 public String transfer(int num){ 154 String str; 155 switch(num){ 156 case 1:str="一";break; 157 case 2:str="二";break; 158 case 3:str="三";break; 159 case 4:str="四";break; 160 case 5:str="五";break; 161 case 6:str="六";break; 162 case 7:str="天";break; 163 default: 164 str=null; 165 } 166 return str; 167 } 168 }
Frame.jave类:
1 import javax.swing.JButton; 2 import javax.swing.JFrame; 3 import javax.swing.JLabel; 4 import javax.swing.JOptionPane; 5 import javax.swing.JTextField; 6 7 import java.awt.Font; 8 import java.awt.event.ActionEvent; 9 import java.awt.event.ActionListener; 10 import java.awt.event.WindowAdapter; 11 import java.awt.event.WindowEvent; 12 import java.text.ParseException; 13 14 15 public class Frame { 16 private JFrame frame = new JFrame("日期计算函数"); 17 private JButton button1 = new JButton("OK"); 18 private JButton button2 = new JButton("Cancel"); 19 private JLabel label1 = new JLabel("年:"); 20 private JLabel label2 = new JLabel("月:"); 21 private JLabel label3 = new JLabel("日:"); 22 private JLabel label4 = new JLabel("上一天是:"); 23 private JLabel label5 = new JLabel("下一天是:"); 24 private JLabel label6 = new JLabel("今天是星期"); 25 private JTextField textfield1 = new JTextField();//年输入框 26 private JTextField textfield2 = new JTextField();//月输入框 27 private JTextField textfield3 = new JTextField();//日输入框 28 29 private JTextField textfield4 = new JTextField();//上一天 30 private JTextField textfield5 = new JTextField();//下一天 31 private JTextField textfield6 = new JTextField();//星期几 32 33 Date date=new Date(); 34 35 public Frame() { 36 37 Font fnt = new Font("TimesRoman", Font.BOLD, 15);//Serief 38 39 label1.setFont(fnt); 40 label2.setFont(fnt); 41 label3.setFont(fnt); 42 label4.setFont(fnt); 43 label5.setFont(fnt); 44 label6.setFont(fnt); 45 textfield4.setFont(fnt); 46 textfield5.setFont(fnt); 47 textfield6.setFont(fnt); 48 49 textfield4.setEditable(false); 50 textfield5.setEditable(false); 51 textfield6.setEditable(false); 52 53 54 button1.addActionListener(new ActionListener() { 55 public void actionPerformed(ActionEvent e) { 56 int year=0,month=0,day=0; 57 try{ 58 year=Integer.parseInt(textfield1.getText()); 59 month=Integer.parseInt(textfield2.getText()); 60 day=Integer.parseInt(textfield3.getText()); 61 } 62 catch(Exception ex){ 63 JOptionPane.showMessageDialog(null, "输入有误,请重新输入!", "错误", JOptionPane.ERROR_MESSAGE); 64 return; 65 } 66 //System.out.println(year+"-"+month+"-"+day); 67 //判断日期是否在规定范围内 68 if(year>2050||year<1912) 69 { 70 JOptionPane.showMessageDialog(null, "年份超出范围!", "错误", JOptionPane.ERROR_MESSAGE); 71 return; 72 } 73 if(month>12||month<1) 74 { 75 JOptionPane.showMessageDialog(null, "月份超出范围!", "错误", JOptionPane.ERROR_MESSAGE); 76 return; 77 } 78 if(day>31||day<1) 79 { 80 JOptionPane.showMessageDialog(null, "日期超出范围!", "错误", JOptionPane.ERROR_MESSAGE); 81 return; 82 } 83 //判断日期合法性 84 String time=year+"-"+month+"-"+day; 85 if(!date.isValidDate(time)){ 86 JOptionPane.showMessageDialog(null, "日期不合法!", "错误", JOptionPane.ERROR_MESSAGE); 87 return; 88 } 89 //当日期合法时,输出上一天和下一天和星期几 90 textfield4.setText(date.lastDay(year,month,day)); 91 textfield5.setText(date.NextDay(year,month,day)); 92 try { 93 textfield6.setText(date.transfer(date.weekDay(year, month, day))); 94 } catch (ParseException e1) { 95 // TODO Auto-generated catch block 96 e1.printStackTrace(); 97 } 98 99 } 100 }); 101 102 button2.addActionListener(new ActionListener() { 103 public void actionPerformed(ActionEvent e) { 104 105 textfield1.setText(""); 106 textfield2.setText(""); 107 textfield3.setText(""); 108 textfield4.setText(""); 109 textfield5.setText(""); 110 textfield6.setText(""); 111 } 112 }); 113 frame.addWindowListener(new WindowAdapter() { 114 public void windowClosing(WindowEvent e) { 115 System.exit(1); 116 } 117 }); 118 //界面设计 119 frame.setLayout(null); 120 label1.setBounds(100, 30, 40, 30); 121 textfield1.setBounds(130, 30, 60, 30); 122 123 label2.setBounds(210, 30, 40, 30); 124 textfield2.setBounds(250, 30, 60, 30); 125 126 label3.setBounds(320,30,40,30); 127 textfield3.setBounds(360, 30, 60, 30); 128 129 button1.setBounds(180,70, 100, 30); 130 button2.setBounds(340, 70, 100, 30); 131 132 label4.setBounds(100,150,100,30); 133 textfield4.setBounds(200, 150,300 ,30); 134 label5.setBounds(100, 190, 100, 30); 135 textfield5.setBounds(200, 190,300 ,30); 136 label6.setBounds(100, 230, 100, 30); 137 textfield6.setBounds(200, 230,300 ,30); 138 139 frame.add(label1); 140 frame.add(label2); 141 frame.add(label3); 142 frame.add(label4); 143 frame.add(label5); 144 frame.add(label6); 145 146 frame.add(textfield1); 147 frame.add(textfield2); 148 frame.add(textfield3); 149 frame.add(textfield4); 150 frame.add(textfield5); 151 frame.add(textfield6); 152 frame.add(button1); 153 frame.add(button2); 154 frame.setSize(600,400); 155 frame.setVisible(true); 156 } 157 158 public static void main(String[] args) { 159 new Frame(); 160 } 161 }