Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

How to validate time in 24-hour format using Regular Expression

// Java program to validate the time in
// 24-hour format using Regular Expression.
 
import java.util.regex.*;
 
class GFG {
 
    // Function to validate the time in 24-hour format
    public static boolean isValidTime(String time)
    {
 
        // Regex to check valid time in 24-hour format.
        String regex = "([01]?[0-9]|2[0-3]):[0-5][0-9]";
 
        // Compile the ReGex
        Pattern p = Pattern.compile(regex);
 
        // If the time is empty
        // return false
        if (time == null) {
            return false;
        }
 
        // Pattern class contains matcher() method
        // to find matching between given time
        // and regular expression.
        Matcher m = p.matcher(time);
 
        // Return if the time
        // matched the ReGex
        return m.matches();
    }
 
    // Driver Code.
    public static void main(String args[])
    {
 
        // Test Case 1:
        String str1 = "13:05";
        System.out.println(str1 + ": "
                           + isValidTime(str1));
 
        // Test Case 2:
        String str2 = "02:15";
        System.out.println(str2 + ": "
                           + isValidTime(str2));
 
        // Test Case 3:
        String str3 = "24:00";
        System.out.println(str3 + ": "
                           + isValidTime(str3));
 
        // Test Case 4:
        String str4 = "10:60";
        System.out.println(str4 + ": "
                           + isValidTime(str4));
 
        // Test Case 5:
        String str5 = "10:15 PM";
        System.out.println(str5 + ": "
                           + isValidTime(str5));
    }
}
Source by www.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #How #validate #time #format #Regular #Expression
ADD COMMENT
Topic
Name
7+3 =