Java: Checking the Date Format of a String against a Specified Requirement
Question:
How can I determine if a given string follows a specific date format in Java? I want to avoid converting the string into a date object and focus solely on validating its format.
Answer:
Java 8 :
Utilize the LocalDateTime, LocalDate, and LocalTime classes from the Java 8 date and time API for comprehensive format validation.
Code:
<code class="java">public static boolean isValidFormat(String format, String value, Locale locale) { LocalDateTime ldt = null; DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format, locale); try { ldt = LocalDateTime.parse(value, formatter); String result = ldt.format(formatter); return result.equals(value); } catch (DateTimeParseException e) { try { LocalDate ld = LocalDate.parse(value, formatter); String result = ld.format(formatter); return result.equals(value); } catch (DateTimeParseException exp) { try { LocalTime lt = LocalTime.parse(value, formatter); String result = lt.format(formatter); return result.equals(value); } catch (DateTimeParseException e2) { // Handle exceptions for debugging purposes } } } return false; }</code>
Usage Example:
<code class="java">System.out.println("isValid - dd/MM/yyyy with 20130925 = " + isValidFormat("dd/MM/yyyy", "20130925", Locale.ENGLISH)); System.out.println("isValid - dd/MM/yyyy with 25/09/2013 = " + isValidFormat("dd/MM/yyyy", "25/09/2013", Locale.ENGLISH)); System.out.println("isValid - dd/MM/yyyy with 25/09/2013 12:13:50 = " + isValidFormat("dd/MM/yyyy", "25/09/2013 12:13:50", Locale.ENGLISH)); System.out.println("isValid - yyyy-MM-dd with 2017-18--15 = " + isValidFormat("yyyy-MM-dd", "2017-18--15", Locale.ENGLISH));</code>
Output:
isValid - dd/MM/yyyy with 20130925 = false isValid - dd/MM/yyyy with 25/09/2013 = true isValid - dd/MM/yyyy with 25/09/2013 12:13:50 = false isValid - yyyy-MM-dd with 2017-18--15 = false
Pre-Java 8:
For earlier versions of Java, use SimpleDateFormat to validate the format.
Code:
<code class="java">public static boolean isValidFormat(String format, String value) { Date date = null; try { SimpleDateFormat sdf = new SimpleDateFormat(format); date = sdf.parse(value); if (!value.equals(sdf.format(date))) { date = null; } } catch (ParseException ex) { ex.printStackTrace(); } return date != null; }</code>
Usage Example and Output:
<code class="java">System.out.println("isValid - dd/MM/yyyy with 20130925 = " + isValidFormat("dd/MM/yyyy", "20130925")); System.out.println("isValid - dd/MM/yyyy with 25/09/2013 = " + isValidFormat("dd/MM/yyyy", "25/09/2013")); System.out.println("isValid - dd/MM/yyyy with 25/09/2013 12:13:50 = " + isValidFormat("dd/MM/yyyy", "25/09/2013 12:13:50"));</code>
isValid - dd/MM/yyyy with 20130925 = false isValid - dd/MM/yyyy with 25/09/2013 = true isValid - dd/MM/yyyy with 25/09/2013 12:13:50 = false
Both solutions return true if the string matches the specified format and false otherwise, providing a reliable method for validating date formats in Java.
The above is the detailed content of How to Validate a Date String Format in Java without Parsing to a Date Object?. For more information, please follow other related articles on the PHP Chinese website!