How to Validate Date Input from User in Specific Formats in Java?
In Java, validating user-provided dates to ensure a specific format is very important for maintaining data integrity and application functionality. Mainly two commonly used approaches are SimpleDateFormat class and DateTimeFormatter class, which validates the expected date format and parses the user input.
In this article, we will learn how to validate a date input into a specific format in Java.
Methods to Validate a Date Input into a Specific Format
There are mainly two ways to validate a date input to ensure it’s in a specific format in Java.
- Using SimpleDateFormat
- Using java.time.DateTimeFormatter
Below is the code implementation of these two approaches.
Program to Validate a Date Input to Ensure it is in a Specific Format in Java
Below are the two methods implementations to validate a date input to ensure it in a specific format.
Method 1: Using SimpleDateFormat
By using SimpleDateFormat we will validate the date format.
Java
// Java Program to validate a date input to ensure // It's in a specific format using SimpleDateFormat import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; // Driver Class public class GFG { // Main Function public static void main(String args[]) { String date = "09-02-2024" ; // input // define the expected date format SimpleDateFormat dateFormat = new SimpleDateFormat( "dd-MM-yyyy" ); // set lenient to false to apply strict date parsing dateFormat.setLenient( false ); try { // parse the user input into a Date object Date parsedDate = dateFormat.parse(date); System.out.println( "Valid date format: " + dateFormat.format(parsedDate)); } catch (ParseException e) { System.out.println( "Invalid date format. yyyy-MM-dd." ); } } } |
Valid date format: 09-02-2024
Explanation of the above Program:
In the above Program,
- We have defined the expected date format as
"dd-MM-yyyy"
using theSimpleDateFormat
class. - We set the lenient property of the
dateFormat
object tofalse
to enforce strict date parsing. - The input date string is
"09-02-2024"
. - We attempt to parse the input string into a
Date
object using the specified format in a try-catch block. - If parsing is successful, we print the valid date format. If parsing fails (due to an invalid format), a
ParseException
is caught, and we print an error message indicating the expected date format (yyyy-MM-dd
).
Method 2: Using java.time.DateTimeFormatter
By using DateTimeFormatter class we will validate the date format.
Java
// Java Program to Validate a Date Input to ensure // It's in a specific format using DateTimeFormatter import java.time.LocalDate; import java.time.format.DateTimeFormatter; // Driver Class public class GFG { // Main Function public static void main(String args[]) { // define the date format String dateFormat = "dd/MM/yyyy" ; // create a DateTimeFormatter object with the specified format DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat); // input date string String input = "2024-02-09" ; try { // parse the input string to LocalDate using the formatter LocalDate date = LocalDate.parse(input, formatter); // print the valid date System.out.println( "Valid date: " + date); } // catch exception if the input format is invalid catch (Exception e) { // print error message for invalid date format System.out.println( "Invalid date format: " + input); } } } |
Invalid date format: 2024-02-09
Explanation of the above Program:
In the above Program,
- We have defined the date format as
"dd/MM/yyyy"
. - We have created a
DateTimeFormatter
object namedformatter
using theDateTimeFormatter.ofPattern
method. - Then we input the date string.
- We attempt to parse the input string into a
LocalDate
object using the specified formatter in a try-catch block. - If parsing is successful, we print the valid date. If parsing fails (due to an invalid format), an exception is caught, and we print an error message indicating the invalid date format.