验证合法日期格式

import java.text.SimpleDateFormat;


public class testriqi {
  private static SimpleDateFormat dateFormat = null;
     static
     {
     // 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写;
         dateFormat = new SimpleDateFormat("yyyy/MM/dd");
         // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
         dateFormat.setLenient(false);
     }

     public static boolean isValidDate(String s)
     {
         try
         {
              dateFormat.parse(s);
              return true;
          }
         catch (Exception e)
         {
             // 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
             return false;
         }
     }
     public static void main(String[] args) {
   // TODO Auto-generated method stub
      String s1="2010/09/04";
      String s2="fsdfsgsadl";
      
      testriqi trq=new testriqi();
      if(trq.isValidDate(s1)==true){
       System.out.println(s1+"  ok");
      }else
       System.out.println(s1+"  no");
      
      if(trq.isValidDate(s2)==true){
       System.out.println(s2+"  ok");
      }else
       System.out.println(s2+"  no");
  }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值