Java code to validate a date and time format. There are two different programs one is to validate a Date format “dd/MM/yyyy” and the other is to validate a Time format “hh: mm am” or “hh: mm pm” in Java. Along with validation time in 24 hr format.
Obtain a date string in the format dd/mm/yyyy. Write code to validate the given date against the given format. The return type of the validateDate method is 1 if the given date format matches the specified format. If the validation fails, return the output as -1.
Input and Output Format to validate Date
- Input is a string.
- Output is a string.
Refer sample output for formatting specifications
Sample Input 1:
12/06/1987
Sample Output 1:
Valid date format
Sample Input 2:
03/1/1987
Sample Output 2:
Invalid date format
Program in Java for Validating Date Format
Following are the steps to validate date format in Java:
- Input a string.
- Pass the string to getValues() method.
- Inside the getValues() method, use the regex pattern to match the string.
- Now, create the object of SimpleDateFormat class using the ‘dd/MM/yyyy’ pattern.
- Here, call the setLenient() method with value false.
- Now, use the parse method to validate the date and return 1 if valid else return -1.
- At last, catch this value in a variable n and check if it is 1 then just print “Valid date format” else print “Invalid date format”.
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s1 = sc.nextLine(); int n = getvalues(s1); if(n==1){ System.out.println("Valid date format"); }else{ System.out.println("Invalid date format"); } } public static int getvalues(String s1) { if(s1.matches("[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{4}")) { SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy"); sdf.setLenient(false); try { Date d1=sdf.parse(s1); return 1; } catch (ParseException e) { return -1; } } else return -1; }}
Output
Validate Time format in Java
Obtain a time string as input in the following format ‘hh:mm am’ or ‘hh:mm pm’. Write code to validate it using the following rules:
- It should be a valid time in 12 hrs format.
- It should have case-insensitive AM or PM.
- If the given time is as per the given rules then return 1 else return -1. If the value returned is 1 then print as valid time else print as Invalid time.
Input and Output Format to validate time format in Java
- Input is a string.
- Output is a string.
Sample Input 1:
09:59 pm
Sample Output 1:
Valid time
Sample Input 2:
10:70 AM
Sample Output 2:
Invalid time
Write a java program to validate the time format in Java
Following are the steps to validate the time format in Java:
- Input a string.
- Now, break the string into tokens by using StringTokenizer class.
- If the count of tokens is equal to 3, then create the object of SimpleDateFormat class with ‘h:mm:ss a’ pattern.
- Set the leniency to false.
- Now, use the parse method to validate the string and print “Valid Time”.
- If the string token count is not equal to 3, then once again create the object of SimpleDateFormat class with “h:mm a” pattern.
- Repeat the above process and print “Valid Time”.
package com.date; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; import java.util.StringTokenizer; public class ValidateTime { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); StringTokenizer st = new StringTokenizer(str, ":"); if (st.countTokens() == 3) { SimpleDateFormat sdf1 = new SimpleDateFormat("h:mm:ss a"); sdf1.setLenient(false); try { Date d2 = sdf1.parse(str); System.out.println("Valid time"); } catch (Exception e) { System.out.println("Invalid time"); } } else { SimpleDateFormat sdf = new SimpleDateFormat("h:mm a"); sdf.setLenient(false); try { Date d1 = sdf.parse(str); System.out.println("Valid time"); } catch (Exception e) { System.out.println("Invalid time"); } } } }
Output
Validate Time in 24 hr format using regex
Obtain a time string as input in the following format “16:00”. Write code to validate it using the following rules:
- It should start with 0-23 or 00-23.
- It should be followed by ‘:’.
- After this, there should be two digits 00 to 59.
- In the end, it should not be, am, pm, AM, and PM.
Input and Output Format to validate time format in Java
- Input is a string.
- Output is a string.
Sample Input 1:
16:00
Sample Output 1:
Valid time
Sample Input 2:
25:00
Sample Output 2:
Invalid time
Program to validate time in 24 hr using regex in Java
Following are the steps to validate the time in 24 hr format in Java:
- Input time in string.
- Now, match the time with the pattern as shown below
String time = ([01]?[0-9]|2[0-3]):[0-5][0-9];
-
- ([01]?[0-9]|2[0-3]) – It represents the start of the pattern.
- : – It represents that the above pattern should be followed by a colon (:).
- [0-5][0-9] – It should end with two digits.
- If the pattern is matched then print “Valid time” else print “Invalid time”.
package com.date; import java.text.SimpleDateFormat; import java.util.Scanner; import java.util.StringTokenizer; public class ValidateTimeFormat { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String time = scanner.next(); if(time.matches("([01]?[0-9]|2[0-3]):[0-5][0-9]")){ System.out.println("Valid Time"); } else{ System.out.println("Invalid Time"); } } }
Output
Thus, in this way, we learn How to validate time format and date format in Java. Along with validating time in 24 hr format using regex.