|
22 | 22 | import java.time.format.DateTimeParseException;
|
23 | 23 | import java.time.temporal.TemporalAccessor;
|
24 | 24 | import java.util.*;
|
| 25 | +import java.util.function.Predicate; |
25 | 26 |
|
26 | 27 | import ghidra.util.exception.AssertException;
|
27 | 28 |
|
@@ -265,21 +266,69 @@ private static Date toDate(LocalDate ld) {
|
265 | 266 | //@formatter:on
|
266 | 267 | }
|
267 | 268 |
|
| 269 | + /** |
| 270 | + * Returns a date for the given numeric values |
| 271 | + * |
| 272 | + * @param year the year |
| 273 | + * @param month the month; 0-based |
| 274 | + * @param day the day of month; 1-based |
| 275 | + * @return the date |
| 276 | + */ |
268 | 277 | public static Date getDate(int year, int month, int day) {
|
269 | 278 | Calendar cal = new GregorianCalendar(year, month, day);
|
270 | 279 | return cal.getTime();
|
271 | 280 | }
|
272 | 281 |
|
| 282 | + /** |
| 283 | + * Returns all days between the two dates. Returns 0 if the same date is passed for both |
| 284 | + * parameters. The order of the dates does not matter. |
| 285 | + * |
| 286 | + * @param date1 the first date |
| 287 | + * @param date2 the second date |
| 288 | + * @return the number of days |
| 289 | + */ |
273 | 290 | public static int getDaysBetween(Date date1, Date date2) {
|
274 |
| - date1 = normalizeDate(date1); |
275 |
| - date2 = normalizeDate(date2); |
| 291 | + return doGetDaysBetween(date1, date2, DateUtils::anyDay); |
| 292 | + } |
| 293 | + |
| 294 | + /** |
| 295 | + * Returns the <b>business days</b> between the two dates. Returns 0 if the same date is |
| 296 | + * passed for both parameters. The order of the dates does not matter. |
| 297 | + * |
| 298 | + * @param date1 the first date |
| 299 | + * @param date2 the second date |
| 300 | + * @return the number of days |
| 301 | + */ |
| 302 | + public static int getBusinessDaysBetween(Date date1, Date date2) { |
| 303 | + return doGetDaysBetween(date1, date2, DateUtils::isBusinessDay); |
| 304 | + } |
| 305 | + |
| 306 | + private static boolean anyDay(Calendar c) { |
| 307 | + return true; |
| 308 | + } |
| 309 | + |
| 310 | + private static boolean isBusinessDay(Calendar c) { |
| 311 | + return !(isWeekend(c) || isHoliday(c)); |
| 312 | + } |
| 313 | + |
| 314 | + private static int doGetDaysBetween(Date date1, Date date2, Predicate<Calendar> dayFilter) { |
| 315 | + |
| 316 | + Date d1 = date1; |
| 317 | + Date d2 = date2; |
| 318 | + if (date1.compareTo(date2) > 0) { |
| 319 | + d1 = date2; |
| 320 | + d2 = date1; |
| 321 | + } |
| 322 | + |
| 323 | + d1 = normalizeDate(d1); |
| 324 | + d2 = normalizeDate(d2); |
276 | 325 |
|
277 | 326 | Calendar cal = new GregorianCalendar();
|
278 |
| - cal.setTime(date1); |
| 327 | + cal.setTime(d1); |
279 | 328 | int days = 0;
|
280 |
| - while (cal.getTime().compareTo(date2) < 0) { |
| 329 | + while (cal.getTime().compareTo(d2) < 0) { |
281 | 330 | cal.add(Calendar.DAY_OF_MONTH, 1);
|
282 |
| - if (!isWeekend(cal) && !isHoliday(cal)) { |
| 331 | + if (dayFilter.test(cal)) { |
283 | 332 | days++;
|
284 | 333 | }
|
285 | 334 | }
|
|
0 commit comments