How to validate Time in 24 Hours format with regular expression
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
The 24-hour clock format is start from 0-23 or 00-23 then a semi colon (:) and follow by 00-59.
Java Regular Expression Example
package com.mkyong.regex;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Time24HoursValidator{
private Pattern pattern;
private Matcher matcher;
private static final String TIME24HOURS_PATTERN =
"([01]?[0-9]|2[0-3]):[0-5][0-9]";
public Time24HoursValidator(){
pattern = Pattern.compile(TIME24HOURS_PATTERN);
}
/**
* Validate time in 24 hours format with regular expression
* @param time time address for validation
* @return true valid time fromat, false invalid time format
*/
public boolean validate(final String time){
matcher = pattern.matcher(time);
return matcher.matches();
}
}
Time format that match:
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 format doesn’t match:
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]
Unit Test – Time24HoursValidatorTest
package com.mkyong.regex;
import org.testng.Assert;
import org.testng.annotations.*;
/**
* Time 24 hours format validator Testing
* @author mkyong
*
*/
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);
}
}
Unit Test – Result
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
PASSED: ValidTime24HoursTest("01:00")
PASSED: ValidTime24HoursTest("02:00")
PASSED: ValidTime24HoursTest("13:00")
PASSED: ValidTime24HoursTest("1:00")
PASSED: ValidTime24HoursTest("2:00")
PASSED: ValidTime24HoursTest("13:01")
PASSED: ValidTime24HoursTest("23:59")
PASSED: ValidTime24HoursTest("15:00")
PASSED: ValidTime24HoursTest("00:00")
PASSED: ValidTime24HoursTest("0:00")
PASSED: InValidTime24HoursTest("24:00")
PASSED: InValidTime24HoursTest("12:60")
PASSED: InValidTime24HoursTest("0:0")
PASSED: InValidTime24HoursTest("13:1")
PASSED: InValidTime24HoursTest("101:00")
===============================================
com.mkyong.regex.Time24HoursValidatorTest
Tests run: 15, Failures: 0, Skips: 0
===============================================
===============================================
mkyong
Total tests run: 15, Failures: 0, Skips: 0
===============================================
Reference
http://en.wikipedia.org/wiki/24-hour_clock
Want to learn more about regular expression? Highly recommend the this and classic book – “Mastering Regular Expression”
in your pattern, 30:00 or 99:00 will pass the validator. I modify it with the pattern (0[0-9]|1[0-9]|2[0-3]):[0-5][0-9] and it’s good for the hour.
and this one fixes also for example:
2:00 which is incorrect
02:00 which is correct
+1
How about this?
/0?[0-9]|1[0-9]|2[0-3]:^[0-5][0-9]$/
I believe this works in Perl so whatever the Java equivalent is for ^, $, or / is.
In Javascript, this time “30:22” it’s validated …
[012]?\d:[012345]\d
Brilliant, thanks.
Thanks
Thanks alot for regex
Gracias locura
God what a pain web development is! Can’t someone develop a framework that’s logical and easy to use. RegExp is a pain in the arse and takes days to learn and I hate it!!1
Hi. Thanks for the regex! I would add a beginning and an end delimiter to this.
/^([01]?[0-9]|2[0-3]):[0-5][0-9]$/
seems like quite a lot coding for task that simple…
Thanks for the time regex.
-Matt
First learn how to implement your logical planning through simple programming. After that try going for programming advanced features else you people would never become a skilled programmer.