-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathCalendarUtils.java
More file actions
102 lines (81 loc) · 3.12 KB
/
CalendarUtils.java
File metadata and controls
102 lines (81 loc) · 3.12 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
package codewithcal.au.calendarappexample;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
public class CalendarUtils
{
public static LocalDate selectedDate;
public static String formattedDate(LocalDate date)
{
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMMM yyyy");
return date.format(formatter);
}
public static String formattedTime(LocalTime time)
{
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm:ss a");
return time.format(formatter);
}
public static String formattedShortTime(LocalTime time)
{
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
return time.format(formatter);
}
public static String monthYearFromDate(LocalDate date)
{
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM yyyy");
return date.format(formatter);
}
public static String monthDayFromDate(LocalDate date)
{
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM d");
return date.format(formatter);
}
public static ArrayList<LocalDate> daysInMonthArray()
{
ArrayList<LocalDate> daysInMonthArray = new ArrayList<>();
YearMonth yearMonth = YearMonth.from(selectedDate);
int daysInMonth = yearMonth.lengthOfMonth();
LocalDate prevMonth = selectedDate.minusMonths(1);
LocalDate nextMonth = selectedDate.plusMonths(1);
YearMonth prevYearMonth = YearMonth.from(prevMonth);
int prevDaysInMonth = prevYearMonth.lengthOfMonth();
LocalDate firstOfMonth = CalendarUtils.selectedDate.withDayOfMonth(1);
int dayOfWeek = firstOfMonth.getDayOfWeek().getValue();
for(int i = 1; i <= 42; i++)
{
if(i <= dayOfWeek)
daysInMonthArray.add(LocalDate.of(prevMonth.getYear(),prevMonth.getMonth(),prevDaysInMonth + i - dayOfWeek));
else if(i > daysInMonth + dayOfWeek)
daysInMonthArray.add(LocalDate.of(nextMonth.getYear(),nextMonth.getMonth(),i - dayOfWeek - daysInMonth));
else
daysInMonthArray.add(LocalDate.of(selectedDate.getYear(),selectedDate.getMonth(),i - dayOfWeek));
}
return daysInMonthArray;
}
public static ArrayList<LocalDate> daysInWeekArray(LocalDate selectedDate)
{
ArrayList<LocalDate> days = new ArrayList<>();
LocalDate current = sundayForDate(selectedDate);
LocalDate endDate = current.plusWeeks(1);
while (current.isBefore(endDate))
{
days.add(current);
current = current.plusDays(1);
}
return days;
}
private static LocalDate sundayForDate(LocalDate current)
{
LocalDate oneWeekAgo = current.minusWeeks(1);
while (current.isAfter(oneWeekAgo))
{
if(current.getDayOfWeek() == DayOfWeek.SUNDAY)
return current;
current = current.minusDays(1);
}
return null;
}
}