Regular expression for matching HH:MM time format

前端 未结 19 2461
甜味超标
甜味超标 2020-11-22 17:25

I want a regexp for matching time in HH:MM format. Here\'s what I have, and it works:

^[0-2][0-3]:[0-5][0-9]$

This matches everything from

19条回答
  •  花落未央
    2020-11-22 18:08

    Declare

    private static final String TIME24HOURS_PATTERN = "([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]";
    
    public boolean validate(final String time) {
        pattern = Pattern.compile(TIME24HOURS_PATTERN);
        matcher = pattern.matcher(time);
        return matcher.matches();
    }
    

    This method return "true" when String match with the Regular Expression.

提交回复
热议问题