-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathDateValidatorLocalDate.java
More file actions
53 lines (46 loc) · 1.64 KB
/
DateValidatorLocalDate.java
File metadata and controls
53 lines (46 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package ssagit.datevalidator;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
public class DateValidatorLocalDate implements DateValidator {
private DateTimeFormatter dateFormatter;
/**
* DateValidatorLocalDate Constructor.
* @param df {@code dateFormatter} object
*/
public DateValidatorLocalDate(DateTimeFormatter df) {
dateFormatter = df;
}
@Override
/**
* Checks if date given is valid, in the following order:
* date - time - locale;
* If all 3 fails, return with invalid, otherwise return results.
*/
public boolean isValid(String dateStr) {
LocalDateTime ldt = null;
try {
ldt = LocalDateTime.parse(dateStr, this.dateFormatter);
String result = ldt.format(dateFormatter);
return result.equals(dateStr);
} catch (DateTimeParseException e) {
try {
LocalDate ld = LocalDate.parse(dateStr, dateFormatter);
String result = ld.format(dateFormatter);
return result.equals(dateStr);
} catch (DateTimeParseException exp) {
try {
LocalTime lt = LocalTime.parse(dateStr, dateFormatter);
String result = lt.format(dateFormatter);
return result.equals(dateStr);
} catch (DateTimeParseException e2) {
// Debugging purposes
e2.printStackTrace();
}
}
}
return false;
}
}