swt中日期的校验(严格校验)

    最近在用swt做一个项目, 其中用到了日期的校验,在网上查了很久发现只有awt和swing的源码,虽然校验的算法是一样的,但是毕竟swt和awt,swing程序结构上有一定的区别,现在将我用swt写日期校验写出来,希望能对大家有一些帮助.
    代码如下:
    import java.util.Calendar;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class CheckDate {
   
    /**
     * 作者blog: blog.csdn.net/guijava
     * 功能:只能输入格式为YYYY-MM-DD的日期,并且范围在年份(1900-2100之间)
     * @param str 将要测试的内容给str
     * @return true为正确,false为错误
     */
    static Text text;
    static String date;
    static Text txt, txtblog;
    public static  boolean isDate(){
        boolean dateFlag;
        int  year = 1800,mouth = 0,day = 0;
        int now;
        date = text.getText();
        Calendar cal=Calendar.getInstance();
        try{
        year=Integer.parseInt(date.substring(0,4));
        mouth=Integer.parseInt(date.substring(5,7));
        day=Integer.parseInt(date.substring(8,10));
        }catch(Exception ee){return false;}
        now=cal.get(Calendar.YEAR);
        if(date.substring(4,5).equals("-") && date.substring(7,8).equals("-") && date.length() == 10){
        if(year>1900&&year<=now){
            if(mouth==1||mouth==3||mouth==5||mouth==7||mouth==8||mouth==10||mouth==12){
                if(day>=1&&day<=31){
                    dateFlag=true;
                }
                else{
                    dateFlag=false;
                }
            }
            else
               if(mouth==4||mouth==6||mouth==9||mouth==11){
           
                        if(day>=1&&day<=30){
                            dateFlag=true;
                        }
                        else{
                            dateFlag=false;
                        }
                }
            else
              if(mouth==2){
                if(day>=1&&day<=29){
                    dateFlag=true;
                }
                else{
                    dateFlag=false;
                }       
                } 
            else{
                dateFlag=false;
            }
        }
        else {
            dateFlag=false;
            }
        return dateFlag;
        }
        else{
          return false;
        }
    }
   
   
   
   
  public static void main(String[] args) {
     
    Display display = new Display();
    final Shell shell = new Shell(display);
   
    Label label = new Label(shell, SWT.NONE);
    label.setText("请输入日期:");
    text = new Text(shell, SWT.BORDER);
    text.setLayoutData(new RowData(200, SWT.DEFAULT));
    text.setTextLimit(10);
   
    Button ok = new Button(shell, SWT.PUSH);
    ok.setText("验 证");
    Button cancel = new Button(shell, SWT.PUSH);
    cancel.setText("清 空");
   
    new Label(shell, SWT.None).setText("您输入的日期");
    txt = new  Text(shell, SWT.BORDER);
    txt.setLayoutData(new RowData(50, SWT.DEFAULT));
   
    new Label(shell, SWT.NONE).setText("欢迎访问我的博客: blog.csdn.net/guijava");
   
    ok.addSelectionListener(new SelectionListener() {
        public void widgetSelected(SelectionEvent event) {
   
            boolean b= isDate();
            if(b==true){
                txt.setText("正确!");
                MessageBox box = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
                box.setText("提示信息");
                box.setMessage("正确");
                box.open();
            }
            else{
                txt.setText("错误!");
                MessageBox box = new MessageBox(shell, SWT.ICON_ERROR | SWT.OK);
                box.setText("提示信息");
                box.setMessage("错误!");
                box.open();
            }
        
        }
        public void widgetDefaultSelected(SelectionEvent e) {
            // TODO Auto-generated method stub           
        }
    });
   
    /**
     *请空按钮
     */
    cancel.addSelectionListener(new SelectionListener() {
        public void widgetSelected(SelectionEvent event) {
           
            text.setText("");
        }
        public void widgetDefaultSelected(SelectionEvent e) {
            // TODO Auto-generated method stub           
        }
    });
      
   
    shell.setDefaultButton(cancel);
    shell.setLayout(new RowLayout());  
    shell.pack();
    shell.open();
    shell.setSize(400, 300);
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
 
}

    其中,isData()是校验算法,日期的输入格式为:YYYY-MM-DD. 例如:2007-09-07就提示正确,其他输入字母,汉字或不符合这个格式的都会提示错误.
    另外本程序对年份做了限制,只能输入1900-2100年,如果不需要这个限制,只需要把            if(year>1900&&year<=now)
    这行代码去掉就可以了,如果有什么疑问,可以到我的blog上留言(blog.csdn.net/guijava),大家一起交流学习.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值