LocalDateTime工具类:根据当前、周、月、季度、半年、年等维度获取时间

时间:2025-04-11 13:09:55

1. 简介

  Java8提供了全新的日期处理包(.*),根据Java8日期新特性封装日期时间工具类LocalDateTimeUtils

2. 相关博客

  Java 8 新特性:日期处理

3. 工具类方法目录

说明 方法名称
当前时间 ()
Date 转 LocalDateTime (new Date()));
LocalDateTime 转 Date (()));
今天开始时间 ()
今天结束时间 ()
昨天开始时间 ()
昨天结束时间 ()
最近7天开始时间 LocalDateTimeUtils.last7DaysStartTime()
最近7天结束时间 LocalDateTimeUtils.last7DaysEndTime()
最近30天开始时间 LocalDateTimeUtils.last30DaysStartTime()
最近30天天结束时间 LocalDateTimeUtils.last30DaysEndTime()
最近一年开始时间 LocalDateTimeUtils.last1YearStartTime()
最近一年结束时间 LocalDateTimeUtils.last1YearEndTime()
本周开始时间 ()
本周结束时间 ()
本月开始时间 ()
本月结束时间 ()
本季度开始时间 ()
本季度结束时间 ()
本半年开始时间 ()
本半年结束时间 ()
本年开始时间 ()
本年结束时间 ()
上周开始时间 ()
上周结束时间 ()
上月开始时间 ()
上月结束时间 ()
上季度开始时间 ()
上季度结束时间 ()
上半年开始时间 ()
上半年结束时间 ()
上一年开始时间 ()
上一年结束时间 ()
下周开始时间 ()
下周结束时间 ()
下月开始时间 ()
下月结束时间 ()
下季度开始时间 ()
下季度结束时间 ()
下半年开始时间 ()
下半年结束时间 ()
下一年开始时间 ()
下一年结束时间 ()

4. 示例代码

import .*;
import ;
import ;
import ;

/**
 * LocalDateTime工具类
 *
 * @author CL
 */
public class LocalDateTimeUtils {

    /**
     * 当前时间
     *
     * @return
     */
    public static LocalDateTime now() {
        return ();
    }

    /**
     * Date 转 LocalDateTime
     *
     * @return
     */
    public static LocalDateTime convert(Date date) {
        return ((()), ());
    }

    /**
     * LocalDateTime 转 Date
     *
     * @return
     */
    public static Date convert(LocalDateTime localDateTime) {
        return ((()).toInstant());
    }

    /**
     * 今天开始时间
     *
     * @return
     */
    public static LocalDateTime todayStartTime() {
        return ((), );
    }

    /**
     * 今天结束时间
     *
     * @return
     */
    public static LocalDateTime todayEndTime() {
        return ((), );
    }

    /**
     * 昨天开始时间
     *
     * @return
     */
    public static LocalDateTime yesterdayStartTime() {
        return (().minus(1L, ), );
    }

    /**
     * 昨天结束时间
     *
     * @return
     */
    public static LocalDateTime yesterdayEndTime() {
        return (().minus(1L, ), );
    }

    /**
     * 最近7天开始时间
     *
     * @return
     */
    public static LocalDateTime last7DaysStartTime() {
        return (().minus(6L, ), );
    }

    /**
     * 最近7天结束时间
     *
     * @return
     */
    public static LocalDateTime last7DaysEndTime() {
        return ((), );
    }

    /**
     * 最近30天开始时间
     *
     * @return
     */
    public static LocalDateTime last30DaysStartTime() {
        return (().minus(29L, ), );
    }

    /**
     * 最近30天结束时间
     *
     * @return
     */
    public static LocalDateTime last30DaysEndTime() {
        return ((), );
    }

    /**
     * 最近一年开始时间
     *
     * @return
     */
    public static LocalDateTime last1YearStartTime() {
        return (().minus(1L, ).plus(1L, ), );
    }

    /**
     * 最近一年结束时间
     *
     * @return
     */
    public static LocalDateTime last1YearEndTime() {
        return ((), );
    }

    /**
     * 本周开始时间
     *
     * @return
     */
    public static LocalDateTime weekStartTime() {
        LocalDate now = ();
        return ((().getValue() - 1), );
    }

    /**
     * 本周结束时间
     *
     * @return
     */
    public static LocalDateTime weekEndTime() {
        LocalDate now = ();
        return ((7 - ().getValue()), );
    }

    /**
     * 本月开始时间
     *
     * @return
     */
    public static LocalDateTime monthStartTime() {
        return (().with(()), );
    }

    /**
     * 本月结束时间
     *
     * @return
     */
    public static LocalDateTime monthEndTime() {
        return (().with(()), );
    }

    /**
     * 本季度开始时间
     *
     * @return
     */
    public static LocalDateTime quarterStartTime() {
        LocalDate now = ();
        Month month = (().firstMonthOfQuarter().getValue());
        return (((), month, 1), );
    }

    /**
     * 本季度结束时间
     *
     * @return
     */
    public static LocalDateTime quarterEndTime() {
        LocalDate now = ();
        Month month = (().firstMonthOfQuarter().getValue()).plus(2L);
        return (((), month, (())), );
    }

    /**
     * 本半年开始时间
     *
     * @return
     */
    public static LocalDateTime halfYearStartTime() {
        LocalDate now = ();
        Month month = (() > 6) ?  : ;
        return (((), month, 1), );
    }

    /**
     * 本半年结束时间
     *
     * @return
     */
    public static LocalDateTime halfYearEndTime() {
        LocalDate now = ();
        Month month = (() > 6) ?  : ;
        return (((), month, (())), );
    }

    /**
     * 本年开始时间
     *
     * @return
     */
    public static LocalDateTime yearStartTime() {
        return (().with(()), );
    }

    /**
     * 本年结束时间
     *
     * @return
     */
    public static LocalDateTime yearEndTime() {
        return (().with(()), );
    }

    /**
     * 上周开始时间
     *
     * @return
     */
    public static LocalDateTime lastWeekStartTime() {
        LocalDate lastWeek = ().minus(1L, );
        return ((().getValue() - 1), );
    }

    /**
     * 上周结束时间
     *
     * @return
     */
    public static LocalDateTime lastWeekEndTime() {
        LocalDate lastWeek = ().minus(1L, );
        return ((7 - ().getValue()), );
    }

    /**
     * 上月开始时间
     *
     * @return
     */
    public static LocalDateTime lastMonthStartTime() {
        return (().minus(1L, ).with(()), );
    }

    /**
     * 上月结束时间
     *
     * @return
     */
    public static LocalDateTime lastMonthEndTime() {
        return (().minus(1L, ).with(()), );
    }

    /**
     * 上季度开始时间
     *
     * @return
     */
    public static LocalDateTime lastQuarterStartTime() {
        LocalDate now = ();
        Month firstMonthOfQuarter = (().firstMonthOfQuarter().getValue());
        Month firstMonthOfLastQuarter = (3L);
        int yearOfLastQuarter = () < 4 ? () - 1 : ();
        return ((yearOfLastQuarter, firstMonthOfLastQuarter, 1), );
    }

    /**
     * 上季度结束时间
     *
     * @return
     */
    public static LocalDateTime lastQuarterEndTime() {
        LocalDate now = ();
        Month firstMonthOfQuarter = (().firstMonthOfQuarter().getValue());
        Month firstMonthOfLastQuarter = (1L);
        int yearOfLastQuarter = () < 4 ? () - 1 : ();
        return ((yearOfLastQuarter, firstMonthOfLastQuarter, ()), );
    }

    /**
     * 上半年开始时间
     *
     * @return
     */
    public static LocalDateTime lastHalfYearStartTime() {
        LocalDate now = ();
        int lastHalfYear = (() > 6) ? () : () - 1;
        Month firstMonthOfLastHalfYear = (() > 6) ?  : ;
        return ((lastHalfYear, firstMonthOfLastHalfYear, 1), );
    }

    /**
     * 上半年结束时间
     *
     * @return
     */
    public static LocalDateTime lastHalfYearEndTime() {
        LocalDate now = ();
        int lastHalfYear = (() > 6) ? () : () - 1;
        Month lastMonthOfLastHalfYear = (() > 6) ?  : ;
        return ((lastHalfYear, lastMonthOfLastHalfYear, ()), );
    }

    /**
     * 上一年开始时间
     *
     * @return
     */
    public static LocalDateTime lastYearStartTime() {
        return (().minus(1L, ).with(()), );
    }

    /**
     * 上一年结束时间
     *
     * @return
     */
    public static LocalDateTime lastYearEndTime() {
        return (().minus(1L, ).with(()), );
    }

    /**
     * 下周开始时间
     *
     * @return
     */
    public static LocalDateTime nextWeekStartTime() {
        LocalDate nextWeek = ().plus(1L, );
        return ((().getValue() - 1), );
    }

    /**
     * 下周结束时间
     *
     * @return
     */
    public static LocalDateTime nextWeekEndTime() {
        LocalDate nextWeek = ().plus(1L, );
        return ((7 - ().getValue()), );
    }

    /**
     * 下月开始时间
     *
     * @return
     */
    public static LocalDateTime nextMonthStartTime() {
        return (().plus(1L, ).with(()), );
    }

    /**
     * 下月结束时间
     *
     * @return
     */
    public static LocalDateTime nextMonthEndTime() {
        return (().plus(1L, ).with(()), );
    }

    /**
     * 下季度开始时间
     *
     * @return
     */
    public static LocalDateTime nextQuarterStartTime() {
        LocalDate now = ();
        Month firstMonthOfQuarter = (().firstMonthOfQuarter().getValue());
        Month firstMonthOfNextQuarter = (3L);
        int yearOfNextQuarter = () > 9 ? () + 1 : ();
        return ((yearOfNextQuarter, firstMonthOfNextQuarter, 1), );
    }

    /**
     * 下季度结束时间
     *
     * @return
     */
    public static LocalDateTime nextQuarterEndTime() {
        LocalDate now = ();
        Month firstMonthOfQuarter = (().firstMonthOfQuarter().getValue());
        Month firstMonthOfNextQuarter = (5L);
        int yearOfNextQuarter = () > 9 ? () + 1 : ();
        return ((yearOfNextQuarter, firstMonthOfNextQuarter, ()), );
    }

    /**
     * 上半年开始时间
     *
     * @return
     */
    public static LocalDateTime nextHalfYearStartTime() {
        LocalDate now = ();
        int nextHalfYear = (() > 6) ? () + 1 : ();
        Month firstMonthOfNextHalfYear = (() > 6) ?  : ;
        return ((nextHalfYear, firstMonthOfNextHalfYear, 1), );
    }

    /**
     * 上半年结束时间
     *
     * @return
     */
    public static LocalDateTime nextHalfYearEndTime() {
        LocalDate now = ();
        int lastHalfYear = (() > 6) ? () + 1 : ();
        Month lastMonthOfNextHalfYear = (() > 6) ?  : ;
        return ((lastHalfYear, lastMonthOfNextHalfYear, ()), );
    }

    /**
     * 下一年开始时间
     *
     * @return
     */
    public static LocalDateTime nextYearStartTime() {
        return (().plus(1L, ).with(()), );
    }

    /**
     * 下一年结束时间
     *
     * @return
     */
    public static LocalDateTime nextYearEndTime() {
        return (().plus(1L, ).with(()), );
    }

    public static void main(String[] args) {
        ("当前时间:" + now());
        ("Date 转 LocalDateTime:" + convert(new Date()));
        ("LocalDateTime 转 Date:" + convert(()));
        ("今天开始时间:" + todayStartTime());
        ("今天结束时间:" + todayEndTime());
        ("昨天开始时间:" + yesterdayStartTime());
        ("昨天结束时间:" + yesterdayEndTime());
        ("最近7天开始时间:" + last7DaysStartTime());
        ("最近7天结束时间:" + last7DaysEndTime());
        ("最近30天开始时间:" + last30DaysStartTime());
        ("最近30天天结束时间:" + last30DaysEndTime());
        ("最近一年开始时间:" + last1YearStartTime());
        ("最近一年结束时间:" + last1YearEndTime());
        ("本周开始时间:" + weekStartTime());
        ("本周结束时间:" + weekEndTime());
        ("本月开始时间:" + monthStartTime());
        ("本月结束时间:" + monthEndTime());
        ("本季度开始时间:" + quarterStartTime());
        ("本季度结束时间:" + quarterEndTime());
        ("本半年开始时间:" + halfYearStartTime());
        ("本半年结束时间:" + halfYearEndTime());
        ("本年开始时间:" + yearStartTime());
        ("本年结束时间:" + yearEndTime());
        ("上周开始时间:" + lastWeekStartTime());
        ("上周结束时间:" + lastWeekEndTime());
        ("上月开始时间:" + lastMonthStartTime());
        ("上月结束时间:" + lastMonthEndTime());
        ("上季度开始时间:" + lastQuarterStartTime());
        ("上季度结束时间:" + lastQuarterEndTime());
        ("上半年开始时间:" + lastHalfYearStartTime());
        ("上半年结束时间:" + lastHalfYearEndTime());
        ("上一年开始时间:" + lastYearStartTime());
        ("上一年结束时间:" + lastYearEndTime());
        ("下周开始时间:" + nextWeekStartTime());
        ("下周结束时间:" + nextWeekEndTime());
        ("下月开始时间:" + nextMonthStartTime());
        ("下月结束时间:" + nextMonthEndTime());
        ("下季度开始时间:" + nextQuarterStartTime());
        ("下季度结束时间:" + nextQuarterEndTime());
        ("下半年开始时间:" + nextHalfYearStartTime());
        ("下半年结束时间:" + nextHalfYearEndTime());
        ("下一年开始时间:" + nextYearStartTime());
        ("下一年结束时间:" + nextYearEndTime());
    }

}