LocalDateTime + hutool 实现本年月周日与上一年月周日

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);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值