-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDateComparison.java
107 lines (86 loc) · 2.73 KB
/
DateComparison.java
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
102
103
104
105
106
107
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateComparison {
static Calendar calendar = Calendar.getInstance();
static SimpleDateFormat formatDate[] = new SimpleDateFormat[] {new SimpleDateFormat ("dd MMM yyyy"), new SimpleDateFormat ("yyyy-MM-dd")};
static Date today = calendar.getTime();
public static boolean beforeToday (String _date1) {
Date date = parse(_date1, formatDate);
if (date.before(today)) {
return true;
} else {
return false;
}
}
public static boolean illegitimateDate (String _date) {
SimpleDateFormat format = new SimpleDateFormat ("dd MMM yyyy");
format.setLenient(false);
Date date;
try {
date = format.parse(_date);
return true;
} catch (ParseException e) {
//System.out.println("Invalid Date");
return false;
}
}
public static boolean beforeDate (String _date1, String _date2) {
Date date1 = parse(_date1, formatDate);
Date date2 = parse(_date2, formatDate);
if (date1.before(date2)) {
return true;
} else {
return false;
}
}
public static long differentValueDate(String _date1,String _date2){
Date date1 = parse(_date1, formatDate);
Date date2 = parse(_date2, formatDate);
long day = (date1.getTime()-date2.getTime())/(24*60*60*1000)>0 ? (date1.getTime()-date2.getTime())/(24*60*60*1000):
(date2.getTime()-date1.getTime())/(24*60*60*1000);
return day;
}
public static Date parse (String _date, DateFormat...formatters) {
Date date = null;
for (DateFormat formatter: formatters) {
try {
date =formatter.parse(_date);
} catch (ParseException e) {
}
}
return date;
}
static Date thirtyDaysAgo() {
Calendar calendar2 = Calendar.getInstance();
calendar2.add(Calendar.DAY_OF_MONTH, -30);
Date past = calendar2.getTime();
return past;
}
static boolean isWithinRange(String date) {
Date toCheck = parse(date, formatDate);
return (toCheck.before(today) && toCheck.after(thirtyDaysAgo()));
}
public static int getYearFromDataStr(String date){
return Integer.parseInt(date.split("-")[0]);
}
public static int getMonthFromDataStr(String date){
return Integer.parseInt(date.split("-")[1]);
}
public static int getDayFromDataStr(String date){
return Integer.parseInt(date.split("-")[2]);
}
public static String getTodayDate(){
return "2017-12-09";
}
public static int getDateGap(String date1,String date2){
//1990-01-01 -->1990-01-10 //return 9
int ans = 0;
ans += (getYearFromDataStr(date2) - getYearFromDataStr(date1)) * 365;
ans += (getMonthFromDataStr(date2) - getMonthFromDataStr(date1)) * 30;
ans += (getDayFromDataStr(date2) - getDayFromDataStr(date1));
return ans;
}
}