`

判断一个日期格式是否正确

    博客分类:
  • java
阅读更多
public static boolean checkDate(String date,String dateFormat){
        if(date == null || date.trim().equals("")){
            return false;
        }

        java.text.SimpleDateFormat phaser = new java.text.SimpleDateFormat(
            dateFormat);
        phaser.setLenient(false);//设置不能自动将不合法日期转换成合法日期

 //例如:将2006-2-31转换为2006-3-3,setLenient默认是设为true,可以自动转换,现在设为false,就阻止了这种转换。
        try{
            if(phaser.parse(date) != null){//如果出现不合法的日期就会出异常
                return true;
            }
        } catch(Exception e){
            e.printStackTrace();
        }
        return false;
    }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics