-
Notifications
You must be signed in to change notification settings - Fork 13
/
TimePeriodsB.java
105 lines (95 loc) · 4 KB
/
TimePeriodsB.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
package com.cj.interview.preparation;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
public class TimePeriodsB implements TimePeriods {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE;
@Override
public List<String> debugTimePeriods(Instant now, ZoneId zoneId) {
List<String> lines = new ArrayList<>();
for (TimePeriod timePeriod : TimePeriod.values()) {
DateRange dateRange = timePeriod.toDateRange(now, zoneId);
String formattedStart = dateTimeFormatter.format(dateRange.start);
String formattedEnd = dateTimeFormatter.format(dateRange.end);
String apiParameters = String.format("start=%s&end=%s", formattedStart, formattedEnd);
String timePeriodName = timePeriod.displayName;
String debugLine = String.format("%-15s -> %s", timePeriodName, apiParameters);
lines.add(debugLine);
}
return lines;
}
static class DateRange {
public final ZonedDateTime start;
public final ZonedDateTime end;
public DateRange(ZonedDateTime start, ZonedDateTime end) {
this.start = start;
this.end = end;
}
}
enum TimePeriod {
LAST_7_DAYS("Last 7 days") {
@Override
public DateRange toDateRange(ZonedDateTime today) {
ZonedDateTime start = today.minusDays(7);
return new DateRange(start, today);
}
},
LAST_30_DAYS("Last 30 Days") {
@Override
public DateRange toDateRange(ZonedDateTime today) {
ZonedDateTime start = today.minusDays(30);
return new DateRange(start, today);
}
},
LAST_QUARTER("Last Quarter") {
@Override
public DateRange toDateRange(ZonedDateTime today) {
int month = today.getMonthValue();
int quarter = (month + 2) / 3;
int monthAtBeginningOfQuarter = quarter * 3 - 2;
ZonedDateTime endQuarter = today.withDayOfMonth(1).withMonth(monthAtBeginningOfQuarter);
ZonedDateTime beginQuarter = endQuarter.minusMonths(3);
return new DateRange(beginQuarter, endQuarter);
}
},
LAST_YEAR("Last Year") {
@Override
public DateRange toDateRange(ZonedDateTime today) {
int year = today.getYear();
ZonedDateTime lastYear = today.withYear(year - 1).withDayOfYear(1);
ZonedDateTime thisYear = today.withYear(year).withDayOfYear(1);
return new DateRange(lastYear, thisYear);
}
},
YEAR_TO_DATE("Year to Date") {
@Override
public DateRange toDateRange(ZonedDateTime today) {
ZonedDateTime beginningOfYear = today.withDayOfYear(1);
return new DateRange(beginningOfYear, today);
}
},
QUARTER_TO_DATE("Quarter to Date") {
@Override
public DateRange toDateRange(ZonedDateTime today) {
int month = today.getMonthValue();
int quarter = (month + 2) / 3;
int monthAtBeginningOfQuarter = quarter * 3 - 2;
ZonedDateTime beginQuarter = today.withDayOfMonth(1).withMonth(monthAtBeginningOfQuarter);
return new DateRange(beginQuarter, today);
}
};
public final String displayName;
TimePeriod(String displayName) {
this.displayName = displayName;
}
public abstract DateRange toDateRange(ZonedDateTime today);
public final DateRange toDateRange(Instant now, ZoneId zoneId) {
ZonedDateTime today = ZonedDateTime.ofInstant(now, zoneId).truncatedTo(ChronoUnit.DAYS);
return toDateRange(today);
}
}
}