-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathValidation.java
More file actions
101 lines (83 loc) · 3.3 KB
/
Copy pathValidation.java
File metadata and controls
101 lines (83 loc) · 3.3 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package metadata;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Pattern;
import static java.util.Calendar.DATE;
import static java.util.Calendar.MONTH;
import static java.util.Calendar.YEAR;
public class Validation {
/**
* Returns the difference in years between "last" and "first" (last-first),
* rounded DOWN (if negative, closer to 0) to the year.
* If the result is non-zero, then positive and negative are determined thus:
* If "last" is actually after "first", the result will be positive;
* otherwise negative.
*
* @param supposedlyLast
* @param supposedlyFirst
* @return
*/
public static int yearsBetween(Date supposedlyLast, Date supposedlyFirst) {
int lastFirstComparison = supposedlyLast.compareTo(supposedlyFirst); // 1 if last > first, 0 if last == first, -1 if last < first
Calendar earlier = null;
Calendar later = null;
if (lastFirstComparison > 0) {
earlier = getCalendar(supposedlyFirst);
later = getCalendar(supposedlyLast);
}
else {
earlier = getCalendar(supposedlyLast);
later = getCalendar(supposedlyFirst);
}
int diff = later.get(YEAR) - earlier.get(YEAR);
if (diff != 0) {
if (earlier.get(MONTH) > later.get(MONTH) ||
(earlier.get(MONTH) == later.get(MONTH) && earlier.get(DATE) > later.get(DATE))) {
diff--; // rounding down to the nearest year
}
}
return diff * lastFirstComparison; // multiply by comparison to get the right sign (plus or minus)
}
public static boolean beforeDate(Date last, Date first) {
Calendar a = getCalendar(first);
Calendar b = getCalendar(last);
return a.before(b);
}
public static Calendar getCalendar(Date date) {
Calendar cal = Calendar.getInstance(Locale.US);
cal.setTime(date);
return cal;
}
public static boolean matchesRegex(String value, String pattern) {
String regex = pattern;
return Pattern.compile(regex).matcher(value).matches();
}
public static boolean blank(String value) {
if ( value == null ) {
return true;
}
value = value.trim();
if ( value.equals("") ) {
return true;
}
return false;
}
public static boolean matchesDatePattern(String value, String pattern) {
String regex = pattern;
if ( pattern.equals("yyyy-MM-dd") ) {
regex = "([12]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\\d|3[01]))";
}
return Pattern.compile(regex).matcher(value).matches();
}
public static int yearsBetween(String last, String first, String format) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
return yearsBetween(simpleDateFormat.parse(last), simpleDateFormat.parse(first));
}
public static boolean beforeDate(String last, String first, String format) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
return beforeDate(simpleDateFormat.parse(last), simpleDateFormat.parse(first));
}
}