java获取月份最后一天Calendar

@Test

    public void get_last_day_of_month(){

        //获取2020-02的最后一天

        String dateString = "2020-02" ;

   

        //获取年月

        int year = Integer.parseInt(dateString.split("-")[0]);

        int month = Integer.parseInt(dateString.split("-")[1]);

   

        //获取 日历 对象

        Calendar calendar = Calendar.getInstance() ;

        //填充年

        calendar.set(Calendar.YEAR , year);

   

        //填充月 calenda里的月份是0-11

        calendar.set(Calendar.MONTH , month - 1);

   

        //获取2020-02日历的最大字段,也就是最大多少天

        int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

   

        //填充 最后一天

        calendar.set(Calendar.DAY_OF_MONTH , maxDay);

 String date = DateFormatUtils.format(calendar.getTimeInMillis(), "yyyy-MM-dd");

        log.info(String.valueOf(date));//[main] INFO com.lyg.rest.DateTimeTest - 2020-02-29

   

        //在上面基础上继续获取最后一天的最后 1s

        //calendar.set(Calendar.HOUR , 23);//下面的log.info会打印出 2020-03-01 11:59:59,因为Calendar.HOUR是12小时制

calendar.set(Calendar.DAY_OF_MONTH,23);//此时才是24h制

        calendar.set(Calendar.MINUTE , 59);

        calendar.set(Calendar.SECOND , 59);

   

        String dateTime = DateFormatUtils.format(calendar.getTimeInMillis(), "yyyy-MM-dd HH:mm:ss");

        log.info(dateTime);

    }

   

List<Jurisdiction> filterJurisdictions = administrativeAreas.stream()

.filter(a -> a.getAdministrativeAreaCode().equals(queryDTO.getAdministrativeCode()))

.collect(Collectors.toList()).get(0).getJurisdictions();

posted @ 2020-11-03 14:24  黑质白章  阅读(7709)  评论(0编辑  收藏  举报