diff --git a/java-dates-2/DaveValidatorUsingLocalDate.java b/java-dates-2/DaveValidatorUsingLocalDate.java
new file mode 100644
index 0000000000..251757df97
--- /dev/null
+++ b/java-dates-2/DaveValidatorUsingLocalDate.java
@@ -0,0 +1,24 @@
+package com.baeldung.date.validation;
+
+import java.time.LocalDate;
+import java.time.format.DateTimeFormatter;
+import java.time.format.DateTimeParseException;
+
+public class DaveValidatorUsingLocalDate implements DateValidator {
+ private DateTimeFormatter dateFormatter;
+
+ public DaveValidatorUsingLocalDate(DateTimeFormatter dateFormatter) {
+ super();
+ this.dateFormatter = dateFormatter;
+ }
+
+ @Override
+ public boolean validate(String dateStr) {
+ try {
+ LocalDate.parse(dateStr, this.dateFormatter);
+ } catch (DateTimeParseException e) {
+ return false;
+ }
+ return true;
+ }
+}
diff --git a/java-dates-2/pom.xml b/java-dates-2/pom.xml
index 9307a794b9..2f861ff5f4 100644
--- a/java-dates-2/pom.xml
+++ b/java-dates-2/pom.xml
@@ -20,6 +20,13 @@
joda-time
${joda-time.version}
+
+
+
+ commons-validator
+ commons-validator
+ 1.6
+
diff --git a/java-dates-2/src/main/java/com/baeldung/date/validation/DateValidator.java b/java-dates-2/src/main/java/com/baeldung/date/validation/DateValidator.java
new file mode 100644
index 0000000000..74c1bf7445
--- /dev/null
+++ b/java-dates-2/src/main/java/com/baeldung/date/validation/DateValidator.java
@@ -0,0 +1,5 @@
+package com.baeldung.date.validation;
+
+public interface DateValidator {
+ public boolean isValid(String dateStr);
+}
diff --git a/java-dates-2/src/main/java/com/baeldung/date/validation/DateValidatorUsingApacheValidator.java b/java-dates-2/src/main/java/com/baeldung/date/validation/DateValidatorUsingApacheValidator.java
new file mode 100644
index 0000000000..f7b2f48d2d
--- /dev/null
+++ b/java-dates-2/src/main/java/com/baeldung/date/validation/DateValidatorUsingApacheValidator.java
@@ -0,0 +1,11 @@
+package com.baeldung.date.validation;
+
+import org.apache.commons.validator.GenericValidator;
+
+public class DateValidatorUsingApacheValidator implements DateValidator {
+
+ @Override
+ public boolean isValid(String dateStr) {
+ return GenericValidator.isDate(dateStr, "yyyy-MM-dd", true);
+ }
+}
diff --git a/java-dates-2/src/main/java/com/baeldung/date/validation/DateValidatorUsingDateFormat.java b/java-dates-2/src/main/java/com/baeldung/date/validation/DateValidatorUsingDateFormat.java
new file mode 100644
index 0000000000..eb0fbdb086
--- /dev/null
+++ b/java-dates-2/src/main/java/com/baeldung/date/validation/DateValidatorUsingDateFormat.java
@@ -0,0 +1,25 @@
+package com.baeldung.date.validation;
+
+import java.text.DateFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+
+public class DateValidatorUsingDateFormat implements DateValidator {
+ private String dateFormat;
+
+ public DateValidatorUsingDateFormat(String dateFormat) {
+ this.dateFormat = dateFormat;
+ }
+
+ @Override
+ public boolean isValid(String dateStr) {
+ DateFormat sdf = new SimpleDateFormat(this.dateFormat);
+ sdf.setLenient(false);
+ try {
+ sdf.parse(dateStr);
+ } catch (ParseException e) {
+ return false;
+ }
+ return true;
+ }
+}
diff --git a/java-dates-2/src/main/java/com/baeldung/date/validation/DateValidatorUsingDateTimeFormatter.java b/java-dates-2/src/main/java/com/baeldung/date/validation/DateValidatorUsingDateTimeFormatter.java
new file mode 100644
index 0000000000..0f68baf06e
--- /dev/null
+++ b/java-dates-2/src/main/java/com/baeldung/date/validation/DateValidatorUsingDateTimeFormatter.java
@@ -0,0 +1,22 @@
+package com.baeldung.date.validation;
+
+import java.time.format.DateTimeFormatter;
+import java.time.format.DateTimeParseException;
+
+public class DateValidatorUsingDateTimeFormatter implements DateValidator {
+ private DateTimeFormatter dateFormatter;
+
+ public DateValidatorUsingDateTimeFormatter(DateTimeFormatter dateFormatter) {
+ this.dateFormatter = dateFormatter;
+ }
+
+ @Override
+ public boolean isValid(String dateStr) {
+ try {
+ this.dateFormatter.parse(dateStr);
+ } catch (DateTimeParseException e) {
+ return false;
+ }
+ return true;
+ }
+}
diff --git a/java-dates-2/src/main/java/com/baeldung/date/validation/DateValidatorUsingLocalDate.java b/java-dates-2/src/main/java/com/baeldung/date/validation/DateValidatorUsingLocalDate.java
new file mode 100644
index 0000000000..f04c2e4185
--- /dev/null
+++ b/java-dates-2/src/main/java/com/baeldung/date/validation/DateValidatorUsingLocalDate.java
@@ -0,0 +1,23 @@
+package com.baeldung.date.validation;
+
+import java.time.LocalDate;
+import java.time.format.DateTimeFormatter;
+import java.time.format.DateTimeParseException;
+
+public class DateValidatorUsingLocalDate implements DateValidator {
+ private DateTimeFormatter dateFormatter;
+
+ public DateValidatorUsingLocalDate(DateTimeFormatter dateFormatter) {
+ this.dateFormatter = dateFormatter;
+ }
+
+ @Override
+ public boolean isValid(String dateStr) {
+ try {
+ LocalDate.parse(dateStr, this.dateFormatter);
+ } catch (DateTimeParseException e) {
+ return false;
+ }
+ return true;
+ }
+}
diff --git a/java-dates-2/src/test/java/com/baeldung/date/validation/DateValidatorUsingApacheValidatorUnitTest.java b/java-dates-2/src/test/java/com/baeldung/date/validation/DateValidatorUsingApacheValidatorUnitTest.java
new file mode 100644
index 0000000000..daa464722a
--- /dev/null
+++ b/java-dates-2/src/test/java/com/baeldung/date/validation/DateValidatorUsingApacheValidatorUnitTest.java
@@ -0,0 +1,20 @@
+package com.baeldung.date.validation;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.apache.commons.validator.GenericValidator;
+import org.junit.Test;
+
+public class DateValidatorUsingApacheValidatorUnitTest {
+
+ @Test
+ public void whenValidDatePassed_ThenTrue() {
+ assertTrue(GenericValidator.isDate("2019-02-28", "yyyy-MM-dd", true));
+ }
+
+ @Test
+ public void whenInvalidDatePassed_ThenFalse() {
+ assertFalse(GenericValidator.isDate("2019-02-29", "yyyy-MM-dd", true));
+ }
+}
diff --git a/java-dates-2/src/test/java/com/baeldung/date/validation/DateValidatorUsingDateFormatUnitTest.java b/java-dates-2/src/test/java/com/baeldung/date/validation/DateValidatorUsingDateFormatUnitTest.java
new file mode 100644
index 0000000000..9b86b3381c
--- /dev/null
+++ b/java-dates-2/src/test/java/com/baeldung/date/validation/DateValidatorUsingDateFormatUnitTest.java
@@ -0,0 +1,23 @@
+package com.baeldung.date.validation;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+public class DateValidatorUsingDateFormatUnitTest {
+
+ @Test
+ public void givenValidator_whenValidDatePassed_ThenTrue() {
+ DateValidator validator = new DateValidatorUsingDateFormat("MM/dd/yyyy");
+
+ assertTrue(validator.isValid("02/28/2019"));
+ }
+
+ @Test
+ public void givenValidator_whenInvalidDatePassed_ThenFalse() {
+ DateValidator validator = new DateValidatorUsingDateFormat("MM/dd/yyyy");
+
+ assertFalse(validator.isValid("02/30/2019"));
+ }
+}
diff --git a/java-dates-2/src/test/java/com/baeldung/date/validation/DateValidatorUsingDateTimeFormatterUnitTest.java b/java-dates-2/src/test/java/com/baeldung/date/validation/DateValidatorUsingDateTimeFormatterUnitTest.java
new file mode 100644
index 0000000000..368b04f8e3
--- /dev/null
+++ b/java-dates-2/src/test/java/com/baeldung/date/validation/DateValidatorUsingDateTimeFormatterUnitTest.java
@@ -0,0 +1,32 @@
+package com.baeldung.date.validation;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.time.format.DateTimeFormatter;
+import java.time.format.ResolverStyle;
+import java.util.Locale;
+
+import org.junit.Test;
+
+public class DateValidatorUsingDateTimeFormatterUnitTest {
+
+ @Test
+ public void givenValidator_whenValidDatePassed_ThenTrue() {
+ DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd", Locale.US)
+ .withResolverStyle(ResolverStyle.STRICT);
+ DateValidator validator = new DateValidatorUsingDateTimeFormatter(dateFormatter);
+
+ assertTrue(validator.isValid("2019-02-28"));
+ }
+
+ @Test
+ public void givenValidator_whenInValidDatePassed_ThenFalse() {
+ DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd", Locale.US)
+ .withResolverStyle(ResolverStyle.STRICT);
+ DateValidator validator = new DateValidatorUsingDateTimeFormatter(dateFormatter);
+
+ assertFalse(validator.isValid("2019-02-30"));
+ }
+
+}
diff --git a/java-dates-2/src/test/java/com/baeldung/date/validation/DateValidatorUsingLocalDateUnitTest.java b/java-dates-2/src/test/java/com/baeldung/date/validation/DateValidatorUsingLocalDateUnitTest.java
new file mode 100644
index 0000000000..63296359db
--- /dev/null
+++ b/java-dates-2/src/test/java/com/baeldung/date/validation/DateValidatorUsingLocalDateUnitTest.java
@@ -0,0 +1,27 @@
+package com.baeldung.date.validation;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.time.format.DateTimeFormatter;
+
+import org.junit.Test;
+
+public class DateValidatorUsingLocalDateUnitTest {
+
+ @Test
+ public void givenValidator_whenValidDatePassed_ThenTrue() {
+ DateTimeFormatter dateFormatter = DateTimeFormatter.BASIC_ISO_DATE;
+ DateValidator validator = new DateValidatorUsingLocalDate(dateFormatter);
+
+ assertTrue(validator.isValid("20190228"));
+ }
+
+ @Test
+ public void givenValidator_whenInValidDatePassed_ThenFalse() {
+ DateTimeFormatter dateFormatter = DateTimeFormatter.BASIC_ISO_DATE;
+ DateValidator validator = new DateValidatorUsingLocalDate(dateFormatter);
+
+ assertFalse(validator.isValid("20190230"));
+ }
+}