前言:
小编找了许多稿子都是用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>