Write a program to read a string representing a date. The date can be in any of the three formats
- dd-MM-yyyy
- dd/MM/yyyy
- dd.MM.yyyy
If the date is valid, print valid else print invalid. The return type (integer) should be based on the validity of the date.
Input and Output Format
- Input consists of a string.
- The output consists of a string.
Refer sample output for formatting specifications
Sample Input 1:
03.12.2013
Sample Output 1:
valid
Sample Input 2:
03$12$2013
Sample Output 2:
Invalid
Java Program for Date Validation using regex
Following are the steps to validate the date against the above-given pattern:
- Input string from the user.
- Pass it to getvalues() method.
- Inside the method, match the string with the regex pattern. If the string is matched then create an instance of SimpleDateFormat and parse the input string to Date and print valid.
- Otherwise, print Invalid.
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); getvalues(s); } public static void getvalues(String s) { if (s.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(s); System.out.println("Valid"); } catch (ParseException e) { System.out.println("Invalid"); } } else if (s.matches("[0-9]{2}[/]{1}[0-9]{2}[/][0-9]{4}")) { SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); sdf.setLenient(false); try { Date d1 = sdf.parse(s); System.out.println("Valid"); } catch (ParseException e) { System.out.println("Invalid"); } } else if (s.matches("[0-9]{2}[-]{1}[0-9]{2}[-][0-9]{4}")) { SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); sdf.setLenient(false); try { Date d1 = sdf.parse(s); System.out.println("Valid"); } catch (ParseException e) { System.out.println("Invalid"); } } else System.out.println("Invalid"); } }
Output
Date Validation using SimpleDateFormat Java 7
Write a program to read a string representing a date. The date should be in the dd-MM-yyyy pattern.
Input and Output Format
- Input consists of a string.
- The output consists of a string.
Refer sample output for formatting specifications
Sample Input 1:
17-01-2020
Sample Output 1:
valid
Sample Input 2:
03/12/2013
Sample Output 2:
Invalid
Following are the steps to validate the date:
- Input string from the user.
- Pass it to getvalues() method.
- Inside the method, First, create an object of SimpleDateFormat class with a given pattern. And, set lenient to false so that the input should strictly match with the given pattern.
- If the date is valid, print Valid otherwise ParseExcepton would be thrown.
package com.demo; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; public class TestJava2 { public static void main(String[] args) throws ParseException { Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); getvalues(s); } private static void getvalues(String s) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); sdf.setLenient(false); try { Date d1 = sdf.parse(s); System.out.println("Valid"); }catch (ParseException e){ System.out.println("Invalid"); } } }
Output
Thus, in this way, we learn how to validate dates against a given pattern in Java.