I wanted to know that is there any method available in Java that can do this.Otherwise I may go for Regex solution.
I have input string from user that can be any characters. And I want to check that the input string is according to my required date format or not.
As I have input 20130925 and my required format is dd/MM/yyyy so, for this case I should get false.
I don't want to convert this date I just want to check whether input string is according to required date format or not.
I have tried following
Date date = null; try { date = new SimpleDateFormat("dd/MM/yyyy").parse("20130925"); } catch (Exception ex) { // do something for invalid dateformat }
but my catch (Exception ex) block is unable to catch any exceptions generated by SimpleDateFormat.Parse();
DateValidator validator = new DateValidatorUsingDateFormat("MM/dd/yyyy"); assertTrue(validator. isValid("02/28/2019")); assertFalse(validator. isValid("02/30/2019")); This was the most common solution before Java 8.
Formatting Dates String pattern = "yyyy-MM-dd"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); String date = simpleDateFormat. format(new Date()); System. out. println(date);
There are two possible solutions: Use a SimpleDateFormat and try to convert the String to Date . If no ParseException is thrown the format is correct. Use a regular expression to parse the String.
Parsing a string back to date/time value in an unknown format is inherently impossible (let's face it, what does 3/3/3
actually mean?!), all we can do is "best effort"
This solution doesn't throw an Exception
, it returns a boolean
, this is by design. Any Exception
s are used purely as a guard mechanism.
Since it's now 2018 and Java 8+ has the date/time API (and the rest have the ThreeTen backport). The solution remains basically the same, but becomes slightly more complicated, as we need to perform checks for:
This makes it look something like...
public static boolean isValidFormat(String format, String value, Locale locale) { LocalDateTime ldt = null; DateTimeFormatter fomatter = DateTimeFormatter.ofPattern(format, locale); try { ldt = LocalDateTime.parse(value, fomatter); String result = ldt.format(fomatter); return result.equals(value); } catch (DateTimeParseException e) { try { LocalDate ld = LocalDate.parse(value, fomatter); String result = ld.format(fomatter); return result.equals(value); } catch (DateTimeParseException exp) { try { LocalTime lt = LocalTime.parse(value, fomatter); String result = lt.format(fomatter); return result.equals(value); } catch (DateTimeParseException e2) { // Debugging purposes //e2.printStackTrace(); } } } return false; }
This makes the following...
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));
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
Simple try and parse the String
to the required Date
using something like SimpleDateFormat
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(); } if (date == null) { // Invalid date format } else { // Valid date format }
You could then simply write a simple method that performed this action and returned true
when ever Date
was not null...
As a suggestion...
Updated with running example
I'm not sure what you are doing, but, the following example...
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class TestDateParser { public static void main(String[] args) { 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")); } 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; } }
Outputs (something like)...
java.text.ParseException: Unparseable date: "20130925" 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 at java.text.DateFormat.parse(DateFormat.java:366) at javaapplication373.JavaApplication373.isValidFormat(JavaApplication373.java:28) at javaapplication373.JavaApplication373.main(JavaApplication373.java:19)
Not correct. For isValidFormat("yyyy-MM-dd", "2017-18--15"); not throw any Exception.
isValid - yyyy-MM-dd", "2017-18--15 = false
Seems to work as expected for me - the method doesn't rely on (nor does it throw) the exception alone to perform it's operation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With