Tutorialspoint examples

Learn java tutorial with examples for beginners online

java regex pattern validate 24 hours format

Regular expressions

Regular expressions represents a sequence of symbols and characters expressing a string or pattern to be searched for within a longer piece of text. The abbreviation for regular expression is regex. In programming regular expressions are mainly used to define constraint on strings like password, email validation.


This regular expression refers to a pattern which start from 0-23 or 00-23 then a semi colon (:) and follow by 00-59.


package com.tutorialspointexamples;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexTest {
	private static final String PATTERN = 
	public static void main(String args[]){ 
		List<String> values = new ArrayList<String>();	
		Pattern pattern = Pattern.compile(PATTERN);
		for (String value : values){
		  Matcher matcher = pattern.matcher(value);
			  System.out.println("Time "+ value +" is valid 24Hours Format");
			  System.out.println("Time "+ value +" is invalid 24Hours Format");


Time 05:00 is valid 24Hours Format
Time 17:01 is valid 24Hours Format
Time 24:00 is invalid 24Hours Format
Time 13:1 is invalid 24Hours Format
Download tutorialspointexamples Android App
Copyright © 2018 Tutorialspoint examples DMCA.com Protection Status