LocalDateTime + hutool 实现
package com.zygh.community.util;
import cn.hutool.core.date.DateUtil;
import java.time.LocalDateTime;
import java.util.Date;
/**
* 时间工具类
* @author admin
*/
public class DateTimeUtil {
//获取本年开始时间
public static LocalDateTime getStartYear() {
return DateUtil.offsetYear(DateUtil.beginOfYear(new Date()), 0).toLocalDateTime();
}
//获取本年结束时间
public static LocalDateTime getEndYear() {
return DateUtil.offsetYear(DateUtil.endOfYear(new Date()), 0).toLocalDateTime();
}
//获取上年开始时间
public static LocalDateTime getStartLastYear() {
return DateUtil.offsetYear(DateUtil.beginOfYear(new Date()), -1).toLocalDateTime();
}
//获取上年结束时间
public static LocalDateTime getEndLastYear() {
return DateUtil.offsetYear(DateUtil.endOfYear(new Date()), -1).toLocalDateTime();
}
//获取本月开始时间
public static LocalDateTime getStartMonth() {
return DateUtil.offsetMonth(DateUtil.beginOfMonth(new Date()), 0).toLocalDateTime();
}
//获取本月开始时间
public static LocalDateTime getEndMonth() {
return DateUtil.offsetMonth(DateUtil.endOfMonth(new Date()), 0).toLocalDateTime();
}
//获取上月开始时间
public static LocalDateTime getStartLastMonth() {
return DateUtil.offsetMonth(DateUtil.beginOfMonth(new Date()), -1).toLocalDateTime();
}
//获取上月开始时间
public static LocalDateTime getEndLastMonth() {
return DateUtil.offsetMonth(DateUtil.endOfMonth(new Date()), -1).toLocalDateTime();
}
//获取本周开始时间
public static LocalDateTime getStartWeek() {
return DateUtil.offsetWeek(DateUtil.beginOfWeek(new Date()), 0).toLocalDateTime();
}
//获取本周开始时间
public static LocalDateTime getEndWeek() {
return DateUtil.offsetWeek(DateUtil.endOfWeek(new Date()), 0).toLocalDateTime();
}
//获取上周开始时间
public static LocalDateTime getStartLastWeek() {
return DateUtil.offsetWeek(DateUtil.beginOfWeek(new Date()), -1).toLocalDateTime();
}
//获取上周开始时间
public static LocalDateTime getEndLastWeek() {
return DateUtil.offsetWeek(DateUtil.endOfWeek(new Date()), -1).toLocalDateTime();
}
//获取当天开始时间
public static LocalDateTime getStartDay() {
return DateUtil.offsetDay(DateUtil.beginOfDay(new Date()), 0).toLocalDateTime();
}
//获取当天结束时间
public static LocalDateTime getEndDay() {
return DateUtil.offsetDay(DateUtil.endOfDay(new Date()), 0).toLocalDateTime();
}
//获取上一天开始时间
public static LocalDateTime getStartLastDay() {
return DateUtil.offsetDay(DateUtil.beginOfDay(new Date()), -1).toLocalDateTime();
}
//获取上一天结束时间
public static LocalDateTime getEndLastDay() {
return DateUtil.offsetDay(DateUtil.endOfDay(new Date()), -1).toLocalDateTime();
}
}
LocalDateTime实现
package com.zygh.community.util;
import java.time.LocalDateTime;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.WeekFields;
import java.util.Locale;
/**
* 时间工具类
* @author admin
*/
public class DateTimeUtil {
// 获取本年开始时间
public static LocalDateTime getStartYear() {
return LocalDate.now().with(TemporalAdjusters.firstDayOfYear()).atStartOfDay();
}
// 获取本年结束时间
public static LocalDateTime getEndYear() {
return LocalDate.now().with(TemporalAdjusters.lastDayOfYear()).atTime(LocalTime.MAX);
}
// 获取上年开始时间
public static LocalDateTime getStartLastYear() {
return LocalDate.now().minusYears(1).with(TemporalAdjusters.firstDayOfYear()).atStartOfDay();
}
// 获取上年结束时间
public static LocalDateTime getEndLastYear() {
return LocalDate.now().minusYears(1).with(TemporalAdjusters.lastDayOfYear()).atTime(LocalTime.MAX);
}
// 获取本月开始时间
public static LocalDateTime getStartMonth() {
return LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay();
}
// 获取本月结束时间
public static LocalDateTime getEndMonth() {
return LocalDate.now().with(TemporalAdjusters.lastDayOfMonth()).atTime(LocalTime.MAX);
}
// 获取上月开始时间
public static LocalDateTime getStartLastMonth() {
return LocalDate.now().minusMonths(1).with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay();
}
// 获取上月结束时间
public static LocalDateTime getEndLastMonth() {
return LocalDate.now().minusMonths(1).with(TemporalAdjusters.lastDayOfMonth()).atTime(LocalTime.MAX);
}
// 获取本周开始时间
public static LocalDateTime getStartWeek() {
return LocalDate.now().with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).atStartOfDay();
}
// 获取本周结束时间
public static LocalDateTime getEndWeek() {
return LocalDate.now().with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)).atTime(LocalTime.MAX);
}
// 获取上周开始时间
public static LocalDateTime getStartLastWeek() {
return LocalDate.now().minusWeeks(1).with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).atStartOfDay();
}
// 获取上周结束时间
public static LocalDateTime getEndLastWeek() {
return LocalDate.now().minusWeeks(1).with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)).atTime(LocalTime.MAX);
}
// 获取当天开始时间
public static LocalDateTime getStartDay() {
return LocalDate.now().atStartOfDay();
}
// 获取当天结束时间
public static LocalDateTime getEndDay() {
return LocalDate.now().atTime(LocalTime.MAX);
}
// 获取上一天开始时间
public static LocalDateTime getStartLastDay() {
return LocalDate.now().minusDays(1).atStartOfDay();
}
// 获取上一天结束时间
public static LocalDateTime getEndLastDay() {
return LocalDate.now().minusDays(1).atTime(LocalTime.MAX);
}
}