/**
* 获取当月开始时间和结束时间
* @return 起止时间
*/
public static Date[] getMonthRangeDate() {
LocalDate today = LocalDate.now();
//本月的第一天
LocalDate firstDay = LocalDate.of(today.getYear(), today.getMonth(), 1);
Date startTimeInMonth = Date.from(firstDay.atStartOfDay(ZoneOffset.ofHours(8)).toInstant());
//本月的最后一天
LocalDate lastDay = today.with(TemporalAdjusters.lastDayOfMonth());
Date endTimeInMonth = Date.from(lastDay.atStartOfDay(ZoneOffset.ofHours(8)).toInstant());
return new Date[]{getDayStartTime(startTimeInMonth),getDayEndTime(endTimeInMonth)};
}
/**
* 获取某个日期的开始时间
* @return 起止时间
*/
public static Date getDayStartTime(Date d) {
Calendar calendar = Calendar.getInstance();
if(null != d) {calendar.setTime(d);}
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
/**
* 获取某个日期的结束时间
* @return 起止时间
*/
public static Date getDayEndTime(Date d) {
Calendar calendar = Calendar.getInstance();
if(null != d) {calendar.setTime(d);}
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
calendar.set(Calendar.MILLISECOND, 999);
return calendar.getTime();
}
引用
java.time.LocalDate;
java.util.Date;
java.time.temporal.TemporalAdjusters;
java.time.ZoneOffset;
java.util.Calendar;
|