-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReportDate.java
144 lines (126 loc) · 4.89 KB
/
ReportDate.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Determines date for specific weekday, example Tuesday, on preceeding weeks
* back to a specified earliest date.
* @author rrusk
*/
//package thirdnextappointment;
import java.io.File;
//import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author rrusk
*/
public class ReportDate {
static final ClassLoader loader = ReportDate.class.getClassLoader();
public ClassLoader getLoader() {
return loader;
}
static private int setWeekDay(String weekDay) {
String matchDay = weekDay.toUpperCase();
int result;
if (matchDay.startsWith("MON")) {
result = Calendar.MONDAY;
} else if (matchDay.startsWith("TUE")) {
result = Calendar.TUESDAY;
} else if (matchDay.startsWith("WED")) {
result = Calendar.WEDNESDAY;
} else if (matchDay.startsWith("THU")) {
result = Calendar.THURSDAY;
} else if (matchDay.startsWith("FRI")) {
result = Calendar.FRIDAY;
} else if (matchDay.startsWith("SAT")) {
result = Calendar.SATURDAY;
} else { // must be Sunday
result = Calendar.SUNDAY;
}
return result;
}
/**
*
* @param earliestDate in format "yyyy-MM-dd"
* @param weekDay with String value, for example "TUESDAY".
* @return array of strings containing dates of specified weekday
*/
static public ArrayList<String> getDates(String earliestDate, String weekDay) {
ArrayList<String> weekdayArrayList = new ArrayList<String>();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date earliestReportDate;
try {
earliestReportDate = dateFormat.parse(earliestDate);
} catch (ParseException ex) {
Logger.getLogger(ReportDate.class.getName()).log(Level.SEVERE, null, ex);
return weekdayArrayList;
}
Calendar cal = Calendar.getInstance();
cal.setTime(earliestReportDate);
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_WEEK);
Date reportDate = calendar.getTime();
int cutoffYear = cal.get(Calendar.YEAR);
int cutoffMonth = cal.get(Calendar.MONTH);
int cutoffDayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
GregorianCalendar gCal =
new GregorianCalendar(cutoffYear, cutoffMonth, cutoffDayOfMonth);
Date cutoffDate = gCal.getTime();
int theWeekday = setWeekDay(weekDay);
boolean includeToday = true;
while (day != theWeekday) {
includeToday = false;
calendar.add(Calendar.DATE, 1);
day = calendar.get(Calendar.DAY_OF_WEEK);
}
if (includeToday) {
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
reportDate = new GregorianCalendar(year, month, dayOfMonth).getTime();
weekdayArrayList.add(dateFormat.format(reportDate));
}
while (reportDate.after(cutoffDate)) {
calendar.add(Calendar.DATE, -7);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
reportDate = new GregorianCalendar(year, month, dayOfMonth).getTime();
if (reportDate.after(cutoffDate) || reportDate.equals(cutoffDate)) {
weekdayArrayList.add(dateFormat.format(reportDate));
}
};
return weekdayArrayList;
}
public static void main(String[] args) {
/* File fd = new File("."); // current directory
File[] files = fd.listFiles();
for (File file : files) {
if (file.isDirectory()) {
System.out.print("directory:");
} else {
System.out.print(" file:");
}
try {
System.out.println(file.getCanonicalPath());
} catch (IOException ex) {
Logger.getLogger(ReportDate.class.getName()).log(Level.SEVERE, null, ex);
}
}
*/
ReportDate rd = new ReportDate();
ArrayList<String> tuesdays = rd.getDates("2014-04-01", "TUESDAY");
String filepath = null;
for (int i = 0; i < tuesdays.size(); i++) {
filepath = "./reports/thirdnextappt_" + tuesdays.get(i) + ".txt";
File f = new File(filepath);
if (!f.exists()) {
System.out.println("file '" + filepath + "' doesn't exist");
}
}
}
}