java获取日历格式的日期数据

  

需求是这样的,需要以整体的样子展示日历,比如,5 *7 或者 6* 7 的格式 

我需要把日期数据获取出来,所以我通过后台的得到需要的数据,分析需要3部分数据

 

 

  • 中间的一部分直接获取一个月的第一天和最后一天,
  • 前一部分和后一部分通过TemporalAdjusters对应的方法获取,
  • TemporalAdjusters.dayOfWeekInMonth 可以获取当前月的第X星期的星期几对应的日期,我们可以利用这个处理。

 

/**
     * <per>
     * <p>返回六行日历系统整月之外的日期区间  </p>
     * <per/>
     * @param date
     * @return java.time.LocalDate[]
     * @throws
     * @Description : TODO Return date range beyond the full month calendar system
     *                   TODO Demo :2020-10-01 -->  [2020-09-27, 2020-09-30, 2020-11-01, 2020-11-07]
     * @author Liruilong
     * @Date 2020/11/5 10:50
     **/
    public static LocalDate[] calendarOsToDay(LocalDate date) {
        LocalDate firstDayOfMonth = date.with(TemporalAdjusters.firstDayOfMonth());
        LocalDate lastDayOfMonth = date.with(TemporalAdjusters.lastDayOfMonth());
        LocalDate monthSaturday = date.with(TemporalAdjusters.dayOfWeekInMonth(6, DayOfWeek.SATURDAY));
        LocalDate monthSunday = date.with(TemporalAdjusters.dayOfWeekInMonth(0, DayOfWeek.SUNDAY));
        LocalDate[] localDate = new LocalDate[]{firstDayOfMonth,firstDayOfMonth,lastDayOfMonth,lastDayOfMonth};
       // 整月之前
        if (firstDayOfMonth.getDayOfWeek().getValue() != DayOfWeek.SUNDAY.getValue()){
            LocalDate lastMonth = LocalDate.of(date.getYear(),  date.getMonthValue() == 1 ? 12 :  date.getMonthValue() - 1  , 1);
            localDate[0] = monthSunday;
            localDate[1] = lastMonth.with(TemporalAdjusters.lastDayOfMonth());
        }
        // 整月之后
         int  day = (int)(localDate[3].toEpochDay() - localDate[0].toEpochDay());
        if (lastDayOfMonth.getDayOfWeek().getValue() != DayOfWeek.SATURDAY.getValue()){
            localDate[2] = date.with(TemporalAdjusters.firstDayOfNextMonth());
            localDate[3] = monthSaturday;
        }else if (day == 34){
            localDate[2] = date.with(TemporalAdjusters.firstDayOfNextMonth());
            localDate[3] =localDate[2].with(TemporalAdjusters.dayOfWeekInMonth(1, DayOfWeek.SATURDAY));
        }else if ( day == 27){
            localDate[2] = date.with(TemporalAdjusters.firstDayOfNextMonth());
            localDate[3] =localDate[2].with(TemporalAdjusters.dayOfWeekInMonth(2, DayOfWeek.SATURDAY));
        }
        return localDate;
    }

 

posted @ 2020-11-09 12:56  山河已无恙  阅读(500)  评论(0编辑  收藏  举报