forked from nus-cs2113-AY2021S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
/
DateUtils.java
72 lines (62 loc) · 2.19 KB
/
DateUtils.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
package athena.common.utils;
import athena.Forecast;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalField;
import java.time.temporal.WeekFields;
import java.util.ArrayList;
import java.util.Locale;
public class DateUtils {
/**
* Utility method to get the first day of this week.
*
* @return The first day of this week.
*/
public static LocalDate getFirstDayOfWeek() {
LocalDate now = LocalDate.now();
TemporalField weekField = WeekFields.of(Locale.getDefault()).dayOfWeek();
return now.with(weekField, 1);
}
/**
* Utility method to get the first day of this month.
*
* @return The first day of this month.
*/
public static LocalDate getFirstDayOfMonth() {
LocalDate now = LocalDate.now();
return now.withDayOfMonth(1);
}
/**
* Gets a list of dates based on the given forecast.
* TODAY - Today's date.
* WEEK - This week's dates starting from the first day of the week (Monday).
* ALL - This month's dates starting from the first day of the month.
*
* @param forecast Forecast for dates
* @return List of dates based on the given forecast
*/
public static ArrayList<LocalDate> getDatesBasedOnForecast(Forecast forecast) {
ArrayList<LocalDate> dates = new ArrayList<LocalDate>();
LocalDate startDate;
LocalDate endDate;
if (forecast == Forecast.WEEK) {
startDate = getFirstDayOfWeek();
endDate = startDate.plusWeeks(1);
} else if (forecast == Forecast.DAY) {
startDate = LocalDate.now();
endDate = startDate.plusDays(1);
} else {
startDate = getFirstDayOfMonth();
endDate = startDate.plusMonths(1);
}
for (LocalDate currentDate = startDate; currentDate.compareTo(endDate) != 0;
currentDate = currentDate.plusDays(1)) {
dates.add(currentDate);
}
return dates;
}
public static String formatDate(LocalDate date) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
return date.format(formatter);
}
}