1.获取当前季度 第一天或最后一天
/**
* 获取季度 第一天或最后一天
*
* @param quarters 0本季度,1下季度,-1上季度 以此类推
* @param isFirst true获取开始时间 false获取结束时间
* @return java.lang.String
*/
public static ZonedDateTime getStartOrEndDayOfQuarter(long quarters, Boolean isFirst) {
System.out.println(LocalDate.now());
LocalDate resDate = LocalDate.now().plusMonths(quarters * 3);
Month month = resDate.getMonth();
Month firstMonthOfQuarter = month.firstMonthOfQuarter();
Month endMonthOfQuarter = Month.of(firstMonthOfQuarter.getValue() + 2);
if (isFirst) {
resDate = LocalDate.of(resDate.getYear(), firstMonthOfQuarter, 1);
} else {
resDate = LocalDate.of(resDate.getYear(), endMonthOfQuarter, endMonthOfQuarter.length(resDate.isLeapYear()));
}
return resDate.atStartOfDay(((ZoneId.systemDefault())));
}
2.获取传入时间季度 第一天或最后一天
/**
* 获取季度 第一天或最后一天
*
* @param isFirst true获取开始时间 false获取结束时间
* @return java.lang.String
*/
public static ZonedDateTime setStartOrEndDayOfQuarter(ZonedDateTime dateTime, Boolean isFirst) {
LocalDate localDate = dateTime.toLocalDate();
Month month = localDate.getMonth();
Month firstMonthOfQuarter = month.firstMonthOfQuarter();
Month endMonthOfQuarter = Month.of(firstMonthOfQuarter.getValue() + 2);
if (isFirst) {
localDate = LocalDate.of(localDate.getYear(), firstMonthOfQuarter,
1);
} else {
localDate = LocalDate.of(localDate.getYear(), endMonthOfQuarter, endMonthOfQuarter.length(localDate.isLeapYear()));
}
return localDate.atStartOfDay((ZoneId.systemDefault()));
}
3.测试代码
public static void main(String[] args) {
// System.out.println("hello");
// ZonedDateTime currentStartQuarter = getStartOrEndDayOfQuarter(0, true);
// System.out.println("本季度开始时间 : " + currentStartQuarter);
// ZonedDateTime currentEndQuarter = getStartOrEndDayOfQuarter(0, false);
// System.out.println("本季度开始时间 : " + currentEndQuarter);
// ZonedDateTime startQuarter = getStartOrEndDayOfQuarter(-1, true);
// System.out.println("本季度开始时间 : " + startQuarter);
// ZonedDateTime endQuarter = getStartOrEndDayOfQuarter(-1, false);
// System.out.println("本季度开始时间 : " + endQuarter);
int monthValue = ZonedDateTime.now().getMonthValue();
int dayValue = ZonedDateTime.now().getDayOfMonth();
System.out.println(monthValue);
System.out.println(dayValue);
ZonedDateTime currentStartQuarter = getStartOrEndDayOfQuarter(0, true).plusMonths(+1).plusDays(+10);
System.out.println(currentStartQuarter);
}