BAEL-3045 Validate Date

Check if a string contains valid date
This commit is contained in:
Chandra Prakash 2019-06-20 00:41:47 -04:00
parent 84b21162ae
commit 3862de2519
11 changed files with 219 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -21,6 +21,13 @@
<version>${joda-time.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-validator/commons-validator -->
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>1.6</version>
</dependency>
<!-- test scoped -->
<dependency>
<groupId>org.assertj</groupId>

View File

@ -0,0 +1,5 @@
package com.baeldung.date.validation;
public interface DateValidator {
public boolean isValid(String dateStr);
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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));
}
}

View File

@ -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"));
}
}

View File

@ -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"));
}
}

View File

@ -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"));
}
}