Skip to content

Commit

Permalink
setLenient(false) for date-time format checking
Browse files Browse the repository at this point in the history
  • Loading branch information
erosb committed Mar 15, 2016
1 parent 79869a6 commit d11439c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,20 @@ public class DateTimeFormatValidator implements FormatValidator {

private static final String DATETIME_FORMAT_STRING_SECFRAC = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";

private SimpleDateFormat dateFormat(final String pattern) {
SimpleDateFormat rval = new SimpleDateFormat(pattern);
rval.setLenient(false);
return rval;
}

@Override
public Optional<String> validate(final String subject) {
try {
new SimpleDateFormat(DATETIME_FORMAT_STRING).parse(subject);
dateFormat(DATETIME_FORMAT_STRING).parse(subject);
return Optional.empty();
} catch (ParseException e) {
try {
new SimpleDateFormat(DATETIME_FORMAT_STRING_SECFRAC).parse(subject);
dateFormat(DATETIME_FORMAT_STRING_SECFRAC).parse(subject);
return Optional.empty();
} catch (ParseException e1) {
return Optional.of(String.format("[%s] is not a valid date-time", subject));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void dateTimeFormatFailure() {

@Test
public void dateTimeSecFracSuccess() {
assertSuccess("2015-02-30T11:00:00.111Z", new DateTimeFormatValidator());
assertSuccess("2015-02-28T11:00:00.111Z", new DateTimeFormatValidator());
}

@Test
Expand All @@ -59,7 +59,7 @@ public void dateTimeSuccess() {

@Test
public void dateTimeZSuccess() {
assertSuccess("2015-02-30T11:00:00Z", new DateTimeFormatValidator());
assertSuccess("2015-02-28T11:00:00Z", new DateTimeFormatValidator());
}

@Test
Expand Down

0 comments on commit d11439c

Please sign in to comment.