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,
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts:
- How to Merge Branch into Master Branch - Git
- Install GitHub Command Line Tool on Mac - Git
- macOS Ventura 13: The default interactive shell is now zsh - zsh
- [Fix] zsh: command not found: python on Mac - Python
- Install and Run Cassandra on Docker Desktop - Docker
- Save webpage as pdf in Google Chrome for Mac OS X - Mac-OS-X
- Fix: Microsoft OneDrive We are currently experiencing technical difficulties - Microsoft
- Java: Convert Byte to Binary String Example - Java