一、类图
二、代码与界面
简单的分层思想,代码目录如下:
计算日期的业务类操作代码:printDate.java;具体包括如下方法。
2.1 增加计算星期几的方法weekDay(),利用蔡勒(Zeller)公式.即w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1:
公式中的符号含义如下,w:星期;c:世纪-1;y:年(两位数);m:月(m大于等于3,小于等于14,即在蔡勒公式中,某年的1、2月要看作上一年的13、14月来计算,比如2003年1月1日要看作2002年的13月1日来计算);d:日;[ ]代表取整,即只要整数部分.(C是世纪数减一,y是年份后两位,M是月份,d是日数.1月和2月要按上一年的13月和 14月来算,这时C和y均按上一年取值.)
算出来的W除以7,余数是几就是星期几.如果余数是0,则为星期日.
static String weekDay(int y,int m, int d) { if(m==1||m==2) { m+=12; y--; } int week=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400+1)%7; String weekstr=""; switch(week) { case 1: weekstr="星期一"; break; case 2: weekstr="星期二"; break; case 3: weekstr="星期三"; break; case 4: weekstr="星期四"; break; case 5: weekstr="星期五"; break; case 6: weekstr="星期六"; break; case 0: weekstr="星期日"; break; } return weekstr; }
2.2 增加计算上一天日期的方法lastDate,代码如下
public String lastDate(int year, int month, int day)//lastDate方法 { String lastDate = null; checkLeapYear(year); check(year, month, day) ; if(month==1 && day==1) { year--; month = 12; day = 31; } else if(day==1 && month!=1) { month = month-1; day = m[month]; } else { day--; } lastDate=year+"年"+month+"月"+day+"日"; m[2] = 28; return lastDate; }
2.3 计算下一天日期方法NextDate
public static String NextDate(int year,int month,int day){ if(((year%4==0&&year%100!=0)||year%400==0)&&month==2) { if(day==29){day=1;month++;} else day++; } else if(month==12) { if(day==31){ day=1; month=1; year++; } else day++; } else if(month==1||month==3||month==5||month==7||month==8||month==10) { if(day==31) { day=1; month++; } else day++; } else if(month==2||month==4||month==6||month==11||month==12&&day==30) { if(month==2&&day==28){ day=1; month++; } else if(day==30) { day=1; month++; } else day++; } else day++; String date=year+"年"+month+"月"+day+"日"; return date; }
3.界面设计代码
package softwareTest; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JButton; import java.awt.BorderLayout; import javax.swing.JEditorPane; import javax.swing.JTextField; import javax.swing.JTextPane; import java.awt.Color; import java.awt.SystemColor; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.ActionEvent; public class windowsDesign { private printDate dat; private JFrame jFrame = new JFrame("输出日期"); private JButton okButton = new JButton("OK"); private JButton cancelButton = new JButton("Cancel"); private JLabel yearLabel = new JLabel("年:"); private JLabel monthLabel = new JLabel("月:"); private JLabel dayLabel = new JLabel("日:"); private JLabel weekDayLabel = new JLabel("这天是星期:"); private JLabel lastDayLabel = new JLabel("上一天是:"); private JLabel nextDayLabel = new JLabel("下一天是:"); private JLabel infoLabel = new JLabel("请输入需要计算的年月日"); private JTextField yearText = new JTextField(10); private JTextField monthText = new JTextField(10); private JTextField dayText = new JTextField(10); private JTextField weekDayText = new JTextField(20); private JTextField lastDayText = new JTextField(20); private JTextField nextDayText = new JTextField(20); public windowsDesign(){ dat = new printDate(); okButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(e.getSource() == okButton){ int year = Integer.parseInt( yearText.getText() ); int month = Integer.parseInt( monthText.getText() ); int day = Integer.parseInt( dayText.getText() ); if(dat.check(year, month, day)==1) { String weekDay = null; weekDay=dat.weekDay(year, month, day); weekDayText.setText(weekDay); } lastDayText.setText( dat.lastDate(year, month, day) ); nextDayText.setText( dat.NextDate(year, month, day) ); } } }); /* * 点击取消按钮清空文本框的内容 */ cancelButton.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { if(e.getSource() == cancelButton){ yearText.setText(""); monthText.setText(""); dayText.setText(""); weekDayText.setText(""); lastDayText.setText(""); nextDayText.setText(""); } } }); jFrame.setLayout(null); infoLabel.setBounds(5,5,220,30);//设置标签的位置与大小 yearLabel.setBounds(5,35,30,30); yearText.setBounds(25,35,50,30); monthLabel.setBounds(75,35,30,30); monthText.setBounds(105,35,30,30); dayLabel.setBounds(135,35,30,30); dayText.setBounds(165,35,30,30); okButton.setBounds(10,70,75,20); cancelButton.setBounds(120,70,75,20); weekDayLabel.setBounds(5,95,100,20); weekDayText.setBounds(110,95,100,20); lastDayLabel.setBounds(5, 125, 100, 20); lastDayText.setBounds(110, 125, 100, 20); nextDayLabel.setBounds(5, 155, 100, 20); nextDayText.setBounds(110, 155, 100, 20); jFrame.add(infoLabel); jFrame.add(yearLabel); jFrame.add(yearText); jFrame.add(monthLabel); jFrame.add(monthText); jFrame.add(dayLabel); jFrame.add(dayText); jFrame.add(okButton); jFrame.add(cancelButton); jFrame.add(weekDayLabel); jFrame.add(weekDayText); jFrame.add(lastDayLabel); jFrame.add(lastDayText); jFrame.add(nextDayLabel); jFrame.add(nextDayText); jFrame.setSize(300,250);//设置窗口大小 jFrame.setVisible(true); } public static void main(String[] args){ new windowsDesign(); } }
4.运行效果
时间: 2024-10-07 21:38:15