(common)(fix):add quarter date period (#632)

This commit is contained in:
Scott
2024-01-16 19:21:35 +08:00
committed by GitHub
parent 7f65057a0f
commit af103f3aa3
2 changed files with 16 additions and 0 deletions

View File

@@ -4,6 +4,7 @@ public enum DatePeriodEnum {
DAY(""),
WEEK(""),
MONTH(""),
QUARTER("季度"),
YEAR("");
private String chName;

View File

@@ -5,6 +5,8 @@ import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Calendar;
@@ -91,6 +93,19 @@ public class DateUtils {
result = result.with(TemporalAdjusters.firstDayOfMonth());
}
break;
case QUARTER:
result = currentDate.minusMonths(intervalDay * 3L);
if (intervalDay == 0) {
TemporalAdjuster firstDayOfQuarter = temporal -> {
LocalDate tempDate = LocalDate.from(temporal);
int month = tempDate.get(ChronoField.MONTH_OF_YEAR);
int firstMonthOfQuarter = ((month - 1) / 3) * 3 + 1;
return tempDate.with(ChronoField.MONTH_OF_YEAR, firstMonthOfQuarter)
.with(TemporalAdjusters.firstDayOfMonth());
};
result = result.with(firstDayOfQuarter);
}
break;
case YEAR:
result = currentDate.minusYears(intervalDay);
if (intervalDay == 0) {