To check if a date of type String is valid or not, you can create a array or a list of date format and iterate through them to check if it is valid or not.
Java Code:package org.code2care.rabbitmq;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
public class JavaDateTimeValidator {
public static void main(String[] args) {
String inputDateString1 = "2023-06-23 11:30 AM";
String inputDateString2 = "32-02-2023";
//you can add your valid supported formats
String[] dateFormatPatterns = {
"yyyy/MM/dd", // Example: 2023/06/30
"dd/MM/yyyy", // Example: 23/06/2023
"MM/dd/yyyy", // Example: 06/23/2023
"yyyy-MM-dd HH:mm a", // Example: 2023-06-30 11:30 AM
"dd/MM/yyyy HH:mm:ss", // Example: 23/06/2023 11:30:00
"MM-dd-yyyy", // Example: 06-30-2023
"MMM dd, yyyy HH:mm:ss", // Example: Jun 23, 2023 11:30:00
"dd MMMM yyyy", // Example: 30 June 2023
"hh:mm a", // Example: 11:30 AM/PM
"HH:mm:ss", // Example: 11:30:00
"hh:mm:ss a" // Example: 11:30:00 AM/PM
};
String res1 = validateDateTimeString(inputDateString1, dateFormatPatterns);
String res2 = validateDateTimeString(inputDateString2, dateFormatPatterns);
System.out.println("The inputted date string " + inputDateString1 + " is: " + res1);
System.out.println("The inputted date string " + inputDateString2 + " is: " + res2);
}
public static String validateDateTimeString(String dateTimeStr, String[] dateFormatPatterns) {
for (String datePattern : dateFormatPatterns) {
try {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(datePattern);
LocalDateTime.parse(dateTimeStr, formatter);
return "valid";
} catch (DateTimeParseException e) {
// just continue
}
}
return "invalid";
}
}
Output:
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- CRUD operations in Spring Boot + JDBC
- Java Check Leap Year - Programs with Code Examples
- [fix] Java JDBC ConnectException: Connection refused
- How to add hours and minutes to Java Instant
- Java Program: Random Number Generator
- Java: The value of the local variable string is not used
- How to get list of all Java versions installed on macOS
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Java + Spring JDBC Template + Gradle Example
- Convert String to LocalDate in Java
- Remove Trailing zeros BigDecimal Java
- Java 8 Predicate Functional Interface isEqual() Method Example
- How to Hardcode Date in Java with Examples
- Java 8: Predicate negate() default Function Example
- Java: Collect Stream as ArrayList or LinkedList
- The Motivation Behind Generics in Java Programming
- How to Add/Subtract Days to the Current Date in Java
- Error: Can not find the tag library descriptor for
- Setting up JUnit 5 dependency with Maven Example
- Run Java Code Every Second
- How to create a tar.gz file using Java
- [Fix] java: integer number too large compilation error
- Java 8: Find the Max value in a List
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- Convert Java Array to ArrayList Code Example
More Posts:
- Program 15: Find String is a Palindrome or Not - 1000+ Python Programs - Python-Programs
- How to Install CVS Version Control on Linux/Ubuntu - Linux
- Convert seconds to minutes using JavaScript - JavaScript
- How to know the Version of Notepad App on Windows 11 - Windows-11
- Comprehensive 256 Ascii code table with Html Hex IBM Microsoft Key - Html
- [Solved] SharePoint Search Internal server error exception - SharePoint
- How To Remove Only Conditional Formatting in Excel - Microsoft
- How to reset AirPods or AirPods Pro using iPhone/iPad or iPod - iOS