`
低调衬着那一抹妖娆
  • 浏览: 30091 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

java 日期校验

阅读更多
/**
     * @param date(2015092510)
     * @return
     * 功能:校验日期
     */
    public boolean validateDate(String date){
        boolean result = false;
        try {
            if(date!=null&&!"".equals(date)&&date.length()==10){
                String year = date.substring(0, 4);
                String month = date.substring(4, 6);
                String day = date.substring(6, 8);
                String hour = date.substring(8);
                if(year.matches("^[0-9]*[1-9][0-9]*$")&&month.matches("^[0-9]*[1-9][0-9]*$")&&day.matches("^[0-9]*[1-9][0-9]*$")&&hour.matches("^\\d+$")){
                    int y = Integer.parseInt(year);
                    int m = Integer.parseInt(month);
                    int d = Integer.parseInt(day);
                    int h = Integer.parseInt(hour);
                    if(y>=1900&&y<=2999){
                        if(m>=1&&m<=12){
                            if(h>=0&&h<=23){
                                if(m==1||m==3||m==5||m==7||m==8||m==10||m==12){
                                    if(d>=1&&d<=31){
                                        result = true;
                                    }
                                } else if(m==4||m==6||m==9||m==11){
                                    if(d>=1&&d<=30){
                                        result = true;
                                    }
                                } else if(m==2){
                                    if(y%4==0){
                                        if(d>=1&&d<=29){
                                            result = true;
                                        }
                                    } else {
                                        if(d>=1&&d<=28){
                                            result = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (RuntimeException e) {
            throw e;
        }
        return result;
    }

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics