How to validate Time in 24 Hours format with regular expression in java

Time in 24-Hour Format Regular Expression Pattern

([01]?[0-9]|2[0-3]):[0-5][0-9]

Description

(                                  #start of group #1
[01]?[0-9]                         #  start with 0-9,1-9,00-09,10-19
|                                  #  or
2[0-3]                             #  start with 20-23
)                                  #end of group #1
:                                  #    follow by a semi colon (:)
[0-5][0-9]                         #      follw by 0..5 and 0..9, which means 00 to 59

Code:

package com.codewr.javacore.regular.expression;

 

import java.util.regex.Matcher;

import java.util.regex.Pattern;

 

/**

 *

 * @author codewr

 */

public class Time24HoursValidator {

 

    private static final String TIME24HOURS_PATTERN

            = "([01]?[0-9]|2[0-3]):[0-5][0-9]";

 

    public static void main(String[] args) {

        Time24HoursValidator time24HoursValidator = new Time24HoursValidator();

        //check  time validate

        boolean isValid = time24HoursValidator.validate("1:00");

        System.out.println("time valid is " + isValid);

    }

 

    /**

     * Validate time with regular expression

     *

     * @param time time for validation

     * @return true valid time, false invalid time

     */

    public boolean validate(final String time) {

        Pattern pattern = Pattern.compile(TIME24HOURS_PATTERN);

        Matcher matcher = pattern.matcher(time);

        return matcher.matches();

 

    }

}

Code UnitTest:

Time valid:

1. “01:00”, “02:00”, “13:00”,
2. “1:00”, “2:00”, “13:01”,
3. “23:59″,”15:00”
4. “00:00″,”0:00”

Time invalid:

1. “24:00” – hour is out of range [0-23]
2. “12:60” – minute is out of range [00-59]
3. “0:0” – invalid format for minute, at least 2 digits
4. “13:1” – invalid format for minute, at least 2 digits
5. “101:00” – hour is out of range [0-23]

import com.codewr.javacore.regular.expression.Time24HoursValidator;

import org.testng.Assert;

import org.testng.annotations.*;

 

/**

 *

 * @author codewr

 */

public class Time24HoursValidatorTest {

 

    private Time24HoursValidator time24HoursValidator;

 

    @BeforeClass

    public void initData() {

        time24HoursValidator = new Time24HoursValidator();

    }

 

    @DataProvider

    public Object[][] ValidTime24HoursProvider() {

        return new Object[][]{

            new Object[]{"01:00"}, new Object[]{"02:00"},

            new Object[]{"13:00"}, new Object[]{"1:00"},

            new Object[]{"2:00"}, new Object[]{"13:01"},

            new Object[]{"23:59"}, new Object[]{"15:00"},

            new Object[]{"00:00"}, new Object[]{"0:00"}

        };

    }

 

    @DataProvider

    public Object[][] InvalidTime24HoursProvider() {

        return new Object[][]{

            new Object[]{"24:00"}, new Object[]{"12:60"},

            new Object[]{"0:0"}, new Object[]{"13:1"},

            new Object[]{"101:00"}

        };

    }

 

    @Test(dataProvider = "ValidTime24HoursProvider")

    public void ValidTime24HoursTest(String time) {

        boolean valid = time24HoursValidator.validate(time);

        System.out.println("Time24Hours is valid : " + time + " , " + valid);

        Assert.assertEquals(true, valid);

    }

 

    @Test(dataProvider = "InvalidTime24HoursProvider",

            dependsOnMethods = "ValidTime24HoursTest")

    public void InValidTime24HoursTest(String time) {

        boolean valid = time24HoursValidator.validate(time);

        System.out.println("Time24Hours is valid : " + time + " , " + valid);

        Assert.assertEquals(false, valid);

    }

}

Output:

Running Time24HoursValidatorTest

Time24Hours is valid : 01:00 , true

Time24Hours is valid : 02:00 , true

Time24Hours is valid : 13:00 , true

Time24Hours is valid : 1:00 , true

Time24Hours is valid : 2:00 , true

Time24Hours is valid : 13:01 , true

Time24Hours is valid : 23:59 , true

Time24Hours is valid : 15:00 , true

Time24Hours is valid : 00:00 , true

Time24Hours is valid : 0:00 , true

Time24Hours is valid : 24:00 , false

Time24Hours is valid : 12:60 , false

Time24Hours is valid : 0:0 , false

Time24Hours is valid : 13:1 , false

Time24Hours is valid : 101:00 , false

Tests run: 15, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.565 sec

 

Results :

 

Tests run: 15, Failures: 0, Errors: 0, Skipped: 0

Note: file pom.xml add maven

<dependency>

<groupId>org.testng</groupId>

<artifactId>testng</artifactId>

<version>6.8</version>

<scope>test</scope>

</dependency>