package com.ruzhoukj.sms.util; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Date; /** * @author 邬吉朋 */ public class DateUtils { /** * 获取当前月份的开始和结束日期。 * * @return 一个包含两个元素的数组,第一个元素是当前月份的开始日期, * 第二个元素是当前月份的结束日期。 */ public static Date[] getStartAndEndDateOfCurrentMonth() { // 获取当前日期 LocalDate today = LocalDate.now(); // 获取本月第一天和最后一天 LocalDate firstDate = today.withDayOfMonth(1); LocalDate lastDate = today.withDayOfMonth(today.lengthOfMonth()); // 转换为LocalDateTime并设置时间 LocalDateTime firstDateTime = firstDate.atStartOfDay(); // 本月第一天的开始时间 LocalDateTime lastDateTime = lastDate.atTime(23, 59, 59); // 本月最后一天的结束时间 // 转换为Date Date startDate = Date.from(firstDateTime.atZone(ZoneId.systemDefault()).toInstant()); Date endDate = Date.from(lastDateTime.atZone(ZoneId.systemDefault()).toInstant()); return new Date[]{startDate, endDate}; } public static void main(String[] args) { Date[] dates = getStartAndEndDateOfCurrentMonth(); System.out.println("Start date of this month: " + dates[0]); System.out.println("End date of this month: " + dates[1]); } }