-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateHelper.groovy
More file actions
162 lines (141 loc) · 5.3 KB
/
DateHelper.groovy
File metadata and controls
162 lines (141 loc) · 5.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import java.time.LocalDate;
import java.time.DayOfWeek;
import java.time.YearMonth;
import java.time.Month;
public class DateHelper
{
public static LocalDate getLocalDate(int year, int month, int day)
{
return LocalDate.of(year, month, day);
}
// If the day falls on a weekend, return date of first workday after weekend
public static LocalDate getFirstWorkDayAtOrAfterDayInMonth(LocalDate inputDate)
{
DayOfWeek day = inputDate.getDayOfWeek();
switch (day) {
case DayOfWeek.SUNDAY:
return inputDate.plusDays(1);
case DayOfWeek.SATURDAY:
return inputDate.plusDays(2);
default:
return inputDate;
}
}
public static LocalDate getEndOfNextWeek(LocalDate inputDate)
{
switch (inputDate.getDayOfWeek()) {
case DayOfWeek.SUNDAY:
return inputDate.plusDays(7);
case DayOfWeek.SATURDAY:
return inputDate.plusDays(8);
case DayOfWeek.FRIDAY:
return inputDate.plusDays(9);
case DayOfWeek.THURSDAY:
return inputDate.plusDays(10);
case DayOfWeek.WEDNESDAY:
return inputDate.plusDays(11);
case DayOfWeek.TUESDAY:
return inputDate.plusDays(12);
case DayOfWeek.MONDAY:
return inputDate.plusDays(13);
}
}
// If the day falls on a weekend, return date of first workday prior to weekend
public static LocalDate getFirstWorkDayAtOrBeforeDayInMonth(LocalDate inputDate)
{
DayOfWeek day = inputDate.getDayOfWeek();
switch (day) {
case DayOfWeek.SUNDAY:
return inputDate.minusDays(2);
case DayOfWeek.SATURDAY:
return inputDate.minusDays(1);
default:
return inputDate;
}
}
public static LocalDate getTaskDateIfTaskNeedsToBeCreated(boolean before, int day)
{
LocalDate inputDate = LocalDate.now();
return getTaskDateIfTaskNeedsToBeCreated(before, day, inputDate);
}
public static LocalDate getTaskDateIfTaskNeedsToBeCreated(boolean before, int day, LocalDate inputDate)
{
LocalDate endOfNextWeek = getEndOfNextWeek(inputDate);
Month currentMonth = inputDate.getMonth();
Month nextMonth = endOfNextWeek.getMonth();
if (currentMonth != nextMonth) {
Month[] months = new Month[2];
months[0] = currentMonth;
months[1] = nextMonth;
return getTaskDateIfTaskNeedsToBeCreated(before, day, months, inputDate);
}
else {
return getTaskDateIfTaskNeedsToBeCreated(before, day, currentMonth, inputDate);
}
}
public static LocalDate getTaskDateIfTaskNeedsToBeCreated(boolean before, int day, Month[] months, LocalDate inputDate)
{
for (Month month : months) {
LocalDate date = getTaskDateIfTaskNeedsToBeCreated(before, day, month, inputDate);
if (date != null) {
return date;
}
}
return null;
}
public static LocalDate getTaskDateIfTaskNeedsToBeCreated(boolean before, int day, Month month, LocalDate inputDate)
{
LocalDate workDay = null;
LocalDate endOfNextWeek = getEndOfNextWeek(inputDate);
boolean sameMonth = inputDate.getMonth() == endOfNextWeek.getMonth();
boolean dayToCome = day >= inputDate.getDayOfMonth() || !sameMonth;
boolean beforeNextWeek = (sameMonth && dayToCome && day <= endOfNextWeek.getDayOfMonth()) || (!sameMonth && day <= endOfNextWeek.getDayOfMonth());
// Only in the specified month
if (inputDate.getMonth() == month || endOfNextWeek.getMonth() == month) {
// It's at or after today in the current month
if (dayToCome && beforeNextWeek) {
if (before) {
workDay = getFirstWorkDayAtOrBeforeDayInMonth(getFirstOccurenceOfDay(inputDate, day));
} else {
LocalDate toReturn = getFirstWorkDayAtOrAfterDayInMonth(getFirstOccurenceOfDay(inputDate, day));
if (toReturn <= endOfNextWeek) {
workDay = toReturn;
}
}
}
}
return workDay;
}
public static int getLastDayOfMonth()
{
LocalDate inputDate = LocalDate.now();
return getLastDayOfMonth(inputDate);
}
public static int getLastDayOfMonth(LocalDate inputDate)
{
return inputDate.lengthOfMonth();
}
public static LocalDate getFirstOccurenceOfDay(int day)
{
LocalDate now = LocalDate.now();
return getFirstOccurenceOfDay(day, now);
}
public static LocalDate getFirstOccurenceOfDay(int day, LocalDate inputDate)
{
int dateDay = inputDate.getDayOfMonth();
// Is today the day?
if (dateDay == day) {
return inputDate;
}
// Did we pass it already this month?
else if (dateDay > day){
YearMonth month = YearMonth.from(inputDate);
LocalDate newDate = month.atDay(day);
return newDate.plusMonths(1);
}
// It's still to come this month
else {
return LocalDate.of(inputDate.getYear(), inputDate.getMonth(), day);
}
}
}