DateTest.java
上传用户:ken5558
上传日期:2022-05-01
资源大小:2k
文件大小:6k
源码类别:

压缩解压

开发平台:

Java

  1. //DateTest程序源码
  2. //程序功能:根据输入的日期,判断是否为合格日期,
  3. //如果合格,则输出相邻的下一个日期
  4. import java.awt.Dimension;
  5. import java.awt.Toolkit;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import javax.swing.*;
  9. public class DateTest extends JFrame{
  10. private static int WIDTH=300;//窗口宽度
  11. private static int HEIGHT=400;//窗口高度
  12. private static JLabel label1;//提示输入日期标签
  13. private static JLabel label2;//提示输出结果标签
  14. private static JTextField inputField;//输入日期框
  15. private static JTextField outputField;//输出结果框
  16. private static JButton button1;//确认键
  17. private static JButton button2;//清空键
  18. private static JButton button3;//退出键
  19. public static void main(String[] args){//程序入口
  20. DateTest dateTest=new DateTest();
  21. }
  22. public DateTest(){//构造函数
  23. setTitle("日期测试");//设置标题
  24. setSize(WIDTH,HEIGHT);//设置窗口大小
  25. setLayout(null);
  26. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//默认关闭窗口
  27. Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();//设置窗口显示在屏幕中央位置
  28.         Dimension frm = this.getSize();
  29.         setLocation( (scr.width - frm.width) / 2,
  30.                     (scr.height - frm.height) / 2 - 18);
  31.         
  32.         label1=new JLabel("请输入日期(8位数字),年份(1812-2012)");//输入日期标签
  33.         label2=new JLabel("测试结果如下所示");//输出结果标签
  34.         inputField=new JTextField(10);//输入日期框
  35.         outputField=new JTextField(10);//输出结果框
  36.         button1=new JButton("确认");//确认键
  37.         button1.addActionListener(new ActionListener(){
  38.          public void actionPerformed(ActionEvent e){
  39.          String date="";//输入日期字符串
  40.          date=inputField.getText();//输入日期框字符串赋值给date变量
  41.          nextDay(date);//调用naxaDay()函数,计算下一个日期
  42.          }
  43.         });
  44.         button2=new JButton("清空");//清空键
  45.         button2.addActionListener(new ActionListener(){
  46.          public void actionPerformed(ActionEvent e) {
  47. inputField.setText("");
  48. outputField.setText("");
  49. }
  50.         });
  51.         button3=new JButton("退出");//退出键
  52.         button3.addActionListener(new ActionListener(){
  53.          public void actionPerformed(ActionEvent E){
  54.          DateTest.this.dispose();
  55.          }
  56.         });
  57.         
  58.         label1.setBounds(20,20,260,40);//为每一个控件设置大小和所处位置
  59.         label2.setBounds(100,140,150,40);
  60.         inputField.setBounds(80,80,140,20);
  61.         outputField.setBounds(60,200,180,20);
  62.         button1.setBounds(50,250,60,30);
  63.         button2.setBounds(110,250,60,30);
  64.         button3.setBounds(170,250,60,30);
  65.      
  66.         add(label1);//添加控件在窗口上
  67.         add(label2);
  68.         add(inputField);
  69.         add(outputField);
  70.         add(button1);
  71.         add(button2);
  72.         add(button3);
  73.         setVisible(true);//显示
  74. }
  75. public void nextDay(String date){//naxtDay()函数,计算输入日期的下一个日期
  76. int year,month,day;//year为日期年份,month为月,day为日
  77. if(date.isEmpty())//如果输入日期为空,提示信息
  78. outputField.setText("输入日期不能为空");
  79. else {
  80.      for (int i = 0; i < date.length(); i++) 
  81.    if (!(date.charAt(i) >= '0' && date.charAt(i) <= '9'))
  82.    outputField.setText( "输入格式不正确");//判断如果输入字符不是数字,则输出格式不正确信息
  83.       else
  84.       {
  85.          if(date.length()!=8)//如果日期长度不为8,提示出错
  86.          outputField.setText( "输入日期长度不正确");
  87.          else
  88.          {
  89.        
  90.        year=Integer.parseInt(date.substring(0, 4));//year为年
  91.        month=Integer.parseInt(date.substring(4, 6));//month为月
  92.        day=Integer.parseInt(date.substring(6, 8));//day为天
  93.        if(year<1812||year>2012)//年份范围在1812到2012之间
  94.        outputField.setText( "输入年份超出范围");
  95.        else {
  96.       if(month<1||month>12)//月份范围在1到12之间
  97.      outputField.setText( "输入月份不正确");
  98.       else
  99.       {
  100.      if(day<1||day>31)//一般日期范围为1到31之间
  101.      outputField.setText("无效输入日期");
  102.  else
  103.  {
  104. //DateDiff(year,month,day);
  105. int days;//days为当前月份的天数
  106.      if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
  107.      days=31;//1,3,5,7,8,10,12月为31天
  108.      else 
  109.      {
  110.      if(month==2)//判断二月天数
  111.      {
  112.      if(year%400==0||(year%100!=0&&year%4==0))
  113.      days=29;//闰年为29天
  114.      else
  115.      days=28;//平年为28天
  116.      }
  117.      else 
  118.      days=30;//2,4,6,9,11月为30天
  119.      }
  120.      if(day>days)//输入天数大于当前月的天数,提示错误
  121.      outputField.setText( "输入日期超出当前月天数");
  122.      else
  123.      {
  124.      if(day==days)//输入天数等于当前月的天数,下一日为下一月的第一天
  125.      {
  126.      month++;//月份加1
  127.      day=1;//天数为1
  128.      }
  129.      else
  130.      day++;//其余天数加一则为下一日
  131.     
  132.          if(month==13)//如果月份为13,则年份加一,月份变为一
  133.          {
  134.          year++;//年份加一
  135.          month=1;//月份为一
  136.          }
  137.          String string1="";//string1为输入日期字符串
  138.              if(month<10&&day<10)
  139.               string1+=year+"0"+month+"0"+day;
  140.              else if(month<10)
  141.               string1+=year+"0"+month+""+day;
  142.              else if(day<10)
  143.               string1+=year+""+month+"0"+day;
  144.              else
  145.              string1+=year+""+month+""+day;
  146.          outputField.setText("下一天是:"+string1); 
  147.    }
  148. }
  149. }
  150. }
  151. }
  152. }
  153.   }
  154. }
  155. }