Write a java program to validate the Date of Birth

Write a program to validate the Date of Birth given as input in String format (MM/dd/yyyy) as per the validation rules given below. Return true for valid dates else return false.
1. Value should not be null
2. month should be between 1-12, date should be between 1-31 and year should be a four digit number.

The return type is TRUE / FALSE.

Input and Output Format:
Input consists of a string.
Output consists of TRUE / FALSE.
Refer sample output for formatting specifications.

Sample Input 1:
12/23/1985

Sample Output 1:
TRUE

Sample Input 2:
31/12/1985

Sample Output 2:
FALSE

java program to validate the Date of Birth

package com.demo;
 
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
 
public class Main {
public static void main(String[] args) {
String str = new String();
Scanner sc = new Scanner(System.in);
str = sc.nextLine();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
sdf.setLenient(false);
try {
Date d1 = sdf.parse(str);
System.out.println("TRUE");
} catch (Exception e) {
System.out.println("FALSE");
}
}
}

Add a Comment

Your email address will not be published. Required fields are marked *

17 + thirteen =