Skip to content

Commit

Permalink
Update VerifyValidDate to use java time libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
spokenbird committed Dec 9, 2024
1 parent 3c2c7bc commit b647926
Showing 1 changed file with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@
import formflow.library.config.submission.Action;
import formflow.library.data.FormSubmission;
import formflow.library.data.Submission;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Component
@SuppressWarnings("unused")
public class VerifyValidDate implements Action {
public static final DateTimeFormatter DTF = DateTimeFormat.forPattern("MM/dd/yyyy");
public static final DateTime MIN_DATE = DTF.parseDateTime("01/01/1901");
public static final DateTimeFormatter DTF = DateTimeFormatter.ofPattern("M/d/yyyy");
public static final LocalDate MIN_DATE = LocalDate.parse("01/01/1901", DTF);

public Map<String, List<String>> runValidation(FormSubmission formSubmission, Submission submission) {
Map<String, Object> inputData = formSubmission.getFormData();
String parentDate = String.format("%s/%s/%s",
inputData.get("dateMonth"),
inputData.get("dateDay"),
inputData.get("dateYear"));
inputData.get("dateMonth"),
inputData.get("dateDay"),
inputData.get("dateYear"));

if (parentDate.equals("null/null/null")) {
return Collections.emptyMap();
Expand All @@ -41,18 +41,18 @@ public Map<String, List<String>> runValidation(FormSubmission formSubmission, Su

private boolean isDateValid(String date) {
try {
DTF.parseDateTime(date);
LocalDate.parse(date, DTF);
return true;
} catch (Exception e) {
} catch (DateTimeParseException e) {
return false;
}
}

private boolean isBetweenNowAndMinDate(String dateAsString) {
try {
DateTime date = DTF.parseDateTime(dateAsString);
return MIN_DATE.isBefore(date.getMillis()) && date.isBeforeNow();
} catch (Exception e) {
LocalDate date = LocalDate.parse(dateAsString, DTF);
return !date.isBefore(MIN_DATE) && !date.isAfter(LocalDate.now());
} catch (DateTimeParseException e) {
return false;
}
}
Expand Down

0 comments on commit b647926

Please sign in to comment.