java根据月份获取最后一天

前言:

小编找了许多稿子都是用Calendar对日期进行操作,今天小编用Hutool工具类里的DateUtil来操作日期。
---------------------------------------------------------------------------------------------------------------------------------

 思绪:

01前端传的

第一步拼接成String日期格式

第二步转为日期格式(月份第一天)

第三步根据月份第一天在获取月份最后一天

代码如下:

package Hutool.DateUtil;


import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import java.util.Date;


public class DateUtli {
    public static void main(String[] args) {
        Date date = new Date();
        int year = DateUtil.year(date);//年份
        String month = "01";//月份

        //yyyyMM----->yyyyMMdd
        String yearMonth = year + month + "01";//year可写死,月份第一天
        DateTime dateTime = DateUtil.parse(yearMonth,DatePattern.PURE_DATE_PATTERN);//转为Date类型
        DateTime ofMonth = DateUtil.endOfMonth(dateTime);//获取月份最后一天(带时分秒)
        DateTime truncate = DateUtil.truncate(ofMonth, DateField.DAY_OF_MONTH);//去时分秒

        System.out.println(dateTime);//转为Date类型
        System.out.println(ofMonth);//月份最后一天(带时分秒)
        System.out.println(truncate);//月份最后一天(去时分秒)

    }

}

结果如下:

依赖如下:

 <!--        hutool工具类-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.16</version>
        </dependency>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值