java代码日期校验

package com.xxxx.util;

/**
 * 输入日期 并进行验证格式是否正确
 */
public class FDate {

    public static void main(String[] args) {
        System.out.println(validate("2018-06-30t"));
    }
    
    /**
     * 检查是否是闰年
     * 
     * @param year
     * @return
     */
    public static boolean run(int year) {
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {// 是闰年
//            System.out.print(year + "是闰年!  ");
            return true;
        } else {
            return false;
        }
    }

    public static boolean validate(String dateStr) {
        String msg ="";
        String[] data = new String[3];
        boolean flag = true; // 若不符合规则将值改为false
        String year = "[0-9]{4}";//
        String month = "[0-9]||0[0-9]||1[12]";//
        String day = "[0-9]||[0-2][0-9]||3[01]";//
        int YEAR = 0;
        String str = dateStr;// 输入的字符串
        data = str.split("[-/.+]");
        // 最基本的检查格式 begin
        if (!data[0].matches(year)) {
            msg = "年不对";
            flag = false;
        }
        if (!data[1].matches(month)) {
            msg = "月不对";
            flag = false;
        }
        if (!data[2].matches(day)) {
            msg = "日不对";
            flag = false;
        }
        // end
        YEAR = Integer.valueOf(data[0]);
        boolean run = run(YEAR);// run 为true是闰年否则是 非闰年
        if (run) {// 闰年
            if (data[1].matches("0[2]||2")) {// 这里是闰年的2月
                if (!data[2].matches("0[1-9]||[1-9]||1[0-9]||2[0-9]")) {
                    flag = false;
                    msg = "2月份的天数不对";
                }
            }
        } else {// 非闰年
            if (data[1].matches("0[2]||2")) {// 这里是平年的2月
                if (!data[2].matches("0[1-9]||[1-9]||1[0-9]||2[0-8]")) {
                    flag = false;
                    msg = "2月份的天数不对";
                }
            }
        }

        // 下面判断除了2月份的大小月天数
        if (data[1].matches("0[13578]||[13578]||1[02]")) {// 这里是大月
            if (!data[2].matches("0[1-9]||[1-9]||[12][0-9]||3[01]")) {
                flag = false;
                msg = data[2] + " 天数不对";
            }
        } else if (data[1].matches("0[469]||[469]||11")) {// 这里是小月
            if (!data[2].matches("0[1-9]||[1-9]||[12][0-9]||30")) {
                flag = false;
                msg = data[2] + " 天数不对";
            }
        }

        if (flag) {
            msg = "日期格式正确";
        }
        
        return flag;
    }

}














public static void main(String[] args){
System.out.println(BCustomerSaleRuleService.chkDateFormat("2018/2-28"));
}




public static boolean chkDateFormat(String date){
try {
// 如果输入日期不是8位的,判定为false.
if(StringUtils.isBlank(date)) {
return false;
}
String[] strs = date.replace("/", "-").split("-");
int year = Integer.parseInt(strs[0]);
int month = Integer.parseInt(strs[1]) - 1;
int day = Integer.parseInt(strs[2]);
Calendar calendar = Calendar.getInstance();
// 当 Calendar 处于 non-lenient 模式时,如果其日历字段中存在任何不一致性,它都会抛出一个异常。
calendar.setLenient(false);
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DATE, day);
// 如果日期错误,执行该语句,必定抛出异常.
calendar.get(Calendar.YEAR);
}
catch(IllegalArgumentException e) {
e.printStackTrace();
return false;
}
return true;
}

 

 

posted @ 2018-09-29 10:54  爱豆  阅读(363)  评论(0)    收藏  举报