Nikos Maravitsas

About Nikos Maravitsas

Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.

Validate Time In 24 Hours Format with Java Regular Expression example

In this tutorial we are going to see how to validate 24 Hours time format with Java Regular Expressions.The basic policy about the 24 hours format is that:

  • It should start with two digits from 00 to 23.
  • It must be followed by ‘:’.
  • It should be followed by two digits from 00 to 59.

 
 
 
 
 
So this is the regular expression we are going to use for 12 hours format validation:

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

You can take a look at the Pattern class documentation to learn how to construct your own regular expressions according to your policy.

1. Validator class

This is the class that we are going to use for 24 hours time format validation.

Time24hFormatValidator.java:

package com.javacodegeeks.java.core;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class Time24hFormatValidator{
 
	private Pattern pattern;
	  private Matcher matcher;

	  private static final String TIME12HOURS_PATTERN = "([01]?[0-9]|2[0-3]):[0-5][0-9]";

	  public Time24hFormatValidator(){
		  pattern = Pattern.compile(TIME12HOURS_PATTERN);
	  }

	 
	  public boolean validate(final String time){		  
		  matcher = pattern.matcher(time);
		  return matcher.matches();	    	    
	  }
}

2. Unit Testing our Time24hFormatValidator class

For unit testing we are going to use JUnit. Unit testing is very important in these situations because they provide good feedback about the correctness of our regular expressions. You can test your program and reassure that your regular expression meets the rules on your policy about the form of 24 hour time format.

This is a basic test class:

Time24hFormatValidatorTest.java:

package com.javacodegeeks.java.core;

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.Collection;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class Time24hFormatValidatorTest {

	private String arg;
	private static Time24hFormatValidator time24hFormatValidator;
	private Boolean expectedValidation;

	public Time24hFormatValidatorTest(String str, Boolean expectedValidation) {
		this.arg = str;
		this.expectedValidation = expectedValidation;
	}

	@BeforeClass
	public static void initialize() {
		time24hFormatValidator = new Time24hFormatValidator();
	}

	@Parameters
	public static Collection<Object[]> data() {
		Object[][] data = new Object[][] {
				{ "10:00 am", false }, // there should be no am|pm
				{ "2.20" , false },    // wrong format
				{ "1 10", false },     // wrong format
				{ "24:20", false },    // out of range
								
				
				{ "09:35", true },                         
				{ "7:30", true },
				{ "23:30", true } };
		    
		return Arrays.asList(data);
	}
	
	@Test
	public void test() {
		Boolean res = time24hFormatValidator.validate(this.arg);
		String validv = (res) ? "valid" : "invalid";
		System.out.println("Time Format "+arg+ " is " + validv);
		assertEquals("Result", this.expectedValidation, res);

	}

}

Output:

Time Format 10:00 am is invalid
Time Format 2.20 is invalid
Time Format 1 10 is invalid
Time Format 24:20 is invalid
Time Format 09:35 is valid
Time Format 7:30 is valid
Time Format 23:30 is valid

 
This was an exampl on how to validate time in 24 Hours format with Java Regular Expression.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close