日期正则, yyyy-MM-dd正则, yyyy-MM-dd HH:mm:ss正则

/**

     * @describe:日期正则表达式    格式如:yyyy-MM-dd

     * @author Lvrenshan

     * @date 2018年4月19日10:36:36

     */

    public static final String YMD_REXP = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";

    public static boolean YMD_REXP(String date){

    Pattern p = Pattern.compile(YMD_REXP);  

    Matcher startM = p.matcher(date);

    boolean b = startM.matches();

    return b; 

    }

    /**

     * @describe:日期正则表达式    格式如:yyyy-MM-dd HH:mm:ss

     * @author Lvrenshan

     * @date 2018年4月19日10:36:36

     */

    public static final String YMDHMS_REXP = "^(((20[0-3][0-9]-(0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|(20[0-3][0-9]-(0[2469]|11)-(0[1-9]|[12][0-9]|30))) (20|21|22|23|[0-1][0-9]):[0-5][0-9]:[0-5][0-9])$";

    public static boolean YMDHMS_REXP(String date){

    return Pattern.compile(YMDHMS_REXP).matcher(date).matches();  

    }
 

猜你喜欢

转载自985571285.iteye.com/blog/2420212