- Visual C++源码
- Visual Basic源码
- C++ Builder源码
- Java源码
- Delphi源码
- C/C++源码
- PHP源码
- Perl源码
- Python源码
- Asm源码
- Pascal源码
- Borland C++源码
- Others源码
- SQL源码
- VBScript源码
- JavaScript源码
- ASP/ASPX源码
- C#源码
- Flash/ActionScript源码
- matlab源码
- PowerBuilder源码
- LabView源码
- Flex源码
- MathCAD源码
- VBA源码
- IDL源码
- Lisp/Scheme源码
- VHDL源码
- Objective-C源码
- Fortran源码
- tcl/tk源码
- QT源码
DateTest.java
资源名称:DateTest.rar [点击查看]
上传用户:ken5558
上传日期:2022-05-01
资源大小:2k
文件大小:6k
源码类别:
压缩解压
开发平台:
Java
- //DateTest程序源码
- //程序功能:根据输入的日期,判断是否为合格日期,
- //如果合格,则输出相邻的下一个日期
- import java.awt.Dimension;
- import java.awt.Toolkit;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.*;
- public class DateTest extends JFrame{
- private static int WIDTH=300;//窗口宽度
- private static int HEIGHT=400;//窗口高度
- private static JLabel label1;//提示输入日期标签
- private static JLabel label2;//提示输出结果标签
- private static JTextField inputField;//输入日期框
- private static JTextField outputField;//输出结果框
- private static JButton button1;//确认键
- private static JButton button2;//清空键
- private static JButton button3;//退出键
- public static void main(String[] args){//程序入口
- DateTest dateTest=new DateTest();
- }
- public DateTest(){//构造函数
- setTitle("日期测试");//设置标题
- setSize(WIDTH,HEIGHT);//设置窗口大小
- setLayout(null);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//默认关闭窗口
- Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();//设置窗口显示在屏幕中央位置
- Dimension frm = this.getSize();
- setLocation( (scr.width - frm.width) / 2,
- (scr.height - frm.height) / 2 - 18);
- label1=new JLabel("请输入日期(8位数字),年份(1812-2012)");//输入日期标签
- label2=new JLabel("测试结果如下所示");//输出结果标签
- inputField=new JTextField(10);//输入日期框
- outputField=new JTextField(10);//输出结果框
- button1=new JButton("确认");//确认键
- button1.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- String date="";//输入日期字符串
- date=inputField.getText();//输入日期框字符串赋值给date变量
- nextDay(date);//调用naxaDay()函数,计算下一个日期
- }
- });
- button2=new JButton("清空");//清空键
- button2.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e) {
- inputField.setText("");
- outputField.setText("");
- }
- });
- button3=new JButton("退出");//退出键
- button3.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent E){
- DateTest.this.dispose();
- }
- });
- label1.setBounds(20,20,260,40);//为每一个控件设置大小和所处位置
- label2.setBounds(100,140,150,40);
- inputField.setBounds(80,80,140,20);
- outputField.setBounds(60,200,180,20);
- button1.setBounds(50,250,60,30);
- button2.setBounds(110,250,60,30);
- button3.setBounds(170,250,60,30);
- add(label1);//添加控件在窗口上
- add(label2);
- add(inputField);
- add(outputField);
- add(button1);
- add(button2);
- add(button3);
- setVisible(true);//显示
- }
- public void nextDay(String date){//naxtDay()函数,计算输入日期的下一个日期
- int year,month,day;//year为日期年份,month为月,day为日
- if(date.isEmpty())//如果输入日期为空,提示信息
- outputField.setText("输入日期不能为空");
- else {
- for (int i = 0; i < date.length(); i++)
- if (!(date.charAt(i) >= '0' && date.charAt(i) <= '9'))
- outputField.setText( "输入格式不正确");//判断如果输入字符不是数字,则输出格式不正确信息
- else
- {
- if(date.length()!=8)//如果日期长度不为8,提示出错
- outputField.setText( "输入日期长度不正确");
- else
- {
- year=Integer.parseInt(date.substring(0, 4));//year为年
- month=Integer.parseInt(date.substring(4, 6));//month为月
- day=Integer.parseInt(date.substring(6, 8));//day为天
- if(year<1812||year>2012)//年份范围在1812到2012之间
- outputField.setText( "输入年份超出范围");
- else {
- if(month<1||month>12)//月份范围在1到12之间
- outputField.setText( "输入月份不正确");
- else
- {
- if(day<1||day>31)//一般日期范围为1到31之间
- outputField.setText("无效输入日期");
- else
- {
- //DateDiff(year,month,day);
- int days;//days为当前月份的天数
- if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
- days=31;//1,3,5,7,8,10,12月为31天
- else
- {
- if(month==2)//判断二月天数
- {
- if(year%400==0||(year%100!=0&&year%4==0))
- days=29;//闰年为29天
- else
- days=28;//平年为28天
- }
- else
- days=30;//2,4,6,9,11月为30天
- }
- if(day>days)//输入天数大于当前月的天数,提示错误
- outputField.setText( "输入日期超出当前月天数");
- else
- {
- if(day==days)//输入天数等于当前月的天数,下一日为下一月的第一天
- {
- month++;//月份加1
- day=1;//天数为1
- }
- else
- day++;//其余天数加一则为下一日
- if(month==13)//如果月份为13,则年份加一,月份变为一
- {
- year++;//年份加一
- month=1;//月份为一
- }
- String string1="";//string1为输入日期字符串
- if(month<10&&day<10)
- string1+=year+"0"+month+"0"+day;
- else if(month<10)
- string1+=year+"0"+month+""+day;
- else if(day<10)
- string1+=year+""+month+"0"+day;
- else
- string1+=year+""+month+""+day;
- outputField.setText("下一天是:"+string1);
- }
- }
- }
- }
- }
- }
- }
- }
- }