Java时间格式验证

package com.shucha.deveiface.biz.utils;

import org.apache.commons.lang3.StringUtils;

/**
 * @author tqf
 * @Description
 * @Version 1.0
 * @since 2020-08-19 14:41
 */
public class time {
    /*1、年份限制在2000~2999这1000年中。
    2、基本格式如下:
    年:yyyy
    月:yyyy-[M]M
    日:yyyy-[M]M-[d]d
    时:yyyy-[M]M-[d]d HH:00:00
    时间:yyyy-[M]M-[d]d HH:mm:ss*/

    private static final String yearPattern = "^2[0-9]{3}$";
    private static final String monthPattern = "^2[0-9]{3}-(0?[1-9]|1[0-2])$";
    private static final String janPattern = "(0?[13578]|1[02])-(0?[1-9]|[12][0-9]|3[01])";
    private static final String febPattern = "0?2-(0?[1-9]|[12][0-9])";
    private static final String aprPattern = "(0?[469]|11)-(0?[1-9]|[12][0-9]|30)";
    private static final String dayPattern = String.format("^2[0-9]{3}-(%s|%s|%s)$", janPattern, febPattern, aprPattern);
    private static final String hourFormat = String.format("^2[0-9]{3}-(%s|%s|%s) ([01][0-9]|2[0-3]):00:00$", febPattern, janPattern, aprPattern);
    private static final String timeFormat = String.format("^2[0-9]{3}-(%s|%s|%s) ([01][0-9]|2[0-3])(:[0-5][0-9]){2}$", febPattern, janPattern, aprPattern);

    public static void main(String[] args) {

        String ary[] = {"gg","2365"};
        //String str = ["董事长"];
        System.out.println(StringUtils.join(ary, "-"));

        System.out.println("2000".matches(yearPattern));
        System.out.println("2999".matches(yearPattern));
        System.out.println("1999".matches(yearPattern));
        System.out.println("20001".matches(yearPattern));
        System.out.println("200".matches(yearPattern));

        System.out.println("2000-11".matches(monthPattern));
        System.out.println("2000-01".matches(monthPattern));
        System.out.println("2000-1".matches(monthPattern));
        System.out.println("2000-13".matches(monthPattern));
        System.out.println("2000-00".matches(monthPattern));

        System.out.println("2000-11-1".matches(dayPattern));
        System.out.println("2000-11-01".matches(dayPattern));
        System.out.println("2000-11-31".matches(dayPattern));
        System.out.println("2000-11-00".matches(dayPattern));
        System.out.println("2000-11-29".matches(dayPattern));
        System.out.println("2000-2-29".matches(dayPattern));
        System.out.println("2000-2-30".matches(dayPattern));

        System.out.println("2000-11-1 18:60:00".matches(hourFormat));
        System.out.println("2000-11-1 18:00:10".matches(hourFormat));
        System.out.println("2000-1-1 18:00:00".matches(hourFormat));
        System.out.println("2000-11-1 28:00:00".matches(hourFormat));
        System.out.println("2000-11-1 08:00:00".matches(hourFormat));
        System.out.println("2000-11-1_18:00:00".matches(hourFormat));

        System.out.println("2000-11-1 08:00:00".matches(timeFormat));
        System.out.println("2000-11-1 08:10:00".matches(timeFormat));
        System.out.println("2000-11-1 08:00:50".matches(timeFormat));
        System.out.println("2000-11-1 08:00:70".matches(timeFormat));
    }
}

posted @   码奴生来只知道前进~  阅读(144)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
点击右上角即可分享
微信分享提示