點選檢視程式碼
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDate;
import java.time.Month;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Test {
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public static class Quarter {
private Month monthS;
private int monthDayS;
private Month monthE;
private int monthDayE;
}
public static void main(String[] args) {
Map<Integer, Quarter> quarterIndex2QuarterDay = new HashMap<>() {{
put(1, Quarter.builder().monthS(Month.JANUARY).monthDayS(1).monthE(Month.MARCH).monthDayE(31).build());
put(2, Quarter.builder().monthS(Month.APRIL).monthDayS(1).monthE(Month.JUNE).monthDayE(30).build());
put(3, Quarter.builder().monthS(Month.JULY).monthDayS(1).monthE(Month.SEPTEMBER).monthDayE(30).build());
put(4, Quarter.builder().monthS(Month.OCTOBER).monthDayS(1).monthE(Month.DECEMBER).monthDayE(31).build());
}};
Map<Integer, List<Integer>> currentQuarterIndexMap = new HashMap<>() {{
put(1, Arrays.asList(2, 3, 4, 1));
put(2, Arrays.asList(3, 4, 1, 2));
put(3, Arrays.asList(4, 1, 2, 3));
put(4, Arrays.asList(1, 2, 3, 4));
}};
int currentQ = (int) Math.ceil(LocalDate.now().getMonthValue() / 3.0);
int currentYear = LocalDate.now().getYear();
List<Integer> qList = currentQuarterIndexMap.get(currentQ);
for (Integer q : qList) {
int year;
if (q <= currentQ) {
year = currentYear;
} else {
year = currentYear - 1;
}
Quarter quarter = quarterIndex2QuarterDay.get(q);
System.err.println(LocalDate.of(year, quarter.getMonthS(), quarter.getMonthDayS()));
System.err.println(LocalDate.of(year, quarter.getMonthE(), quarter.getMonthDayE()));
}
}
}