获取四个季度的最后一天

package test;
import java.time.LocalDate;
import java.time.Month;
import java.time.Year;

public class lastjidu {
    public static void main(String[] args) {

        LocalDate lastDayOfCurrentQuarter = getLastDayOfCurrentQuarter();
        System.out.println("当前季度的最后一天是:" + lastDayOfCurrentQuarter);






    }

    public static LocalDate getLastDayOfCurrentQuarter() {
        LocalDate currentDate = LocalDate.now();

//         直接可以获得 月份(1-12)
        Month currentMonth = currentDate.getMonth();
        int currentQuarter = (currentMonth.getValue() -1) / 3 +1;
        System.out.println(currentMonth.getValue());//9 月份
        System.out.println(currentQuarter);//4 第4季度

//    Month 类型
        Month lastMonthOfQuarter = Month.of((currentQuarter ) * 3 );
        System.out.println(lastMonthOfQuarter);//DECEMBER
//        int 类型
        int lastMonthOfQuarter_geshu = Month.of((currentQuarter ) * 3 ).getValue();
        System.out.println(lastMonthOfQuarter_geshu);//12 季度最后一天

//        currentDate.withMonth( ) 的参数为int 类型  更改 currentDate的月份
//        withDayOfMonth            更改 currentDate的天数
//        lastMonthOfQuarter.length(false)   返回当前月的最大天数,false是当为二月时是否为闰年
        LocalDate lastDayOfQuarter = currentDate.withMonth(lastMonthOfQuarter_geshu).withDayOfMonth(lastMonthOfQuarter.length(false));



        return lastDayOfQuarter;
    }
}

你可能感兴趣的:(java,开发语言)