时间工具类


前言

在日常开发中,会涉及到数据统计的业务,数据统计的根本是按照严格的时间计算数据和统计数据,因此在开发过程会,需要获取各种各样的时间点或者时间段,本篇文章对时间处理做出一个简单的封装类,后期会继续完善


一、时间工具类封装

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;

public class DateTimeUtils {
   //昨天日期
   public static LocalDate getYesterday(LocalDate date) {
      return date.minusDays(1);
   }

   //明天日期
   public static LocalDate getTomorrow(LocalDate date) {
      return date.plusDays(1);
   }

   //上周日期:指的是7天前的日期
   public static LocalDate getLastWeek(LocalDate date) {
      return date.minusWeeks(1);
   }


   //下周日期:指的是7天后的日期
   public static LocalDate getNextWeek(LocalDate date) {
      return date.plusWeeks(1);
   }

   //上个月日期
   public static LocalDate getLastMonth(LocalDate date) {
      return date.minusMonths(1);
   }

   //下个月日期
   public static LocalDate getNextMonth(LocalDate date) {
      return date.plusMonths(1);
   }

   //去年日期
   public static LocalDate getLastYear(LocalDate date) {
      return date.minusYears(1);
   }
   //明年日期
   public static LocalDate getNextYear(LocalDate date) {
      return date.plusYears(1);
   }

   //获取本周开始日期
    public static LocalDate getCurrentWeekStart(LocalDate date) {
      return date.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
   }

   //获取本周结束日期
    public static LocalDate getCurrentWeekEnd(LocalDate date) {
      return date.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
   }


   //获取当前月开始日期
   public static LocalDate getCurrentMonthStart(LocalDate date) {
      return date.withDayOfMonth(1);
   }

   //获取当前月结束日期
   public static LocalDate getCurrentMonthEnd(LocalDate date) {
      return date.withDayOfMonth(date.lengthOfMonth());
   }

   //获取当前季度开始日期,要求1-3月返回1季度,4-6月返回2季度,7-9月返回3季度,10-12月返回4季度
   public static LocalDate getCurrentQuarterStart(LocalDate date) {
      int month = date.getMonthValue();
      int quarter = (month - 1) / 3;
      return date.withMonth((quarter * 3) + 1).withDayOfMonth(1);
   }
   //获取当前日期季度结束日期,要求1-3月返回1季度,4-6月返回2季度,7-9月返回3季度,10-12月返回4季度
   public static LocalDate getCurrentQuarterEnd(LocalDate date) {
      int month = date.getMonthValue();
      int quarter = (month - 1) / 3;
      return date.withMonth((quarter * 3) + 3).with(TemporalAdjusters.lastDayOfMonth());
   }

   //获取当前年日期
    public static LocalDate getYear(LocalDate date) {
      return date.withDayOfMonth(1).withMonth(1);
   }

   //获取月日期
    public static LocalDate getMonth(LocalDate date) {
      return date.withDayOfMonth(1);
   }
   //获取日日期
    public static LocalDate getDay(LocalDate date) {
      return date;
   }

   //获取日期的当前星期数
   public static DayOfWeek getCurrentWeek(LocalDate date) {
      return date.getDayOfWeek();
   }

  //根据日期获取第几季度,要求1-3月返回1季度,4-6月返回2季度,7-9月返回3季度,10-12月返回4季度
   public static int getQuarter(LocalDate date) {
      int month = date.getMonthValue();
      int quarter = (month - 1) / 3;
      return quarter + 1;
   }


   //当前时间的前一天时间
   public static LocalDateTime getYesterdayTime(LocalDateTime dateTime) {
      return dateTime.minusDays(1);
   }

   //当前时间的后一天时间
   public static LocalDateTime getTomorrowTime(LocalDateTime dateTime) {
      return dateTime.plusDays(1);
   }

   //获取当天开始时间
   public static LocalDateTime getTodayStartTime(LocalDateTime dateTime) {
      return dateTime.withHour(0).withMinute(0).withSecond(0).withNano(0);
   }

   //获取当天结束时间
   public static LocalDateTime getTodayEndTime(LocalDateTime dateTime) {
      return dateTime.withHour(23).withMinute(59).withSecond(59).withNano(999999999);
   }

   //获取当前时间的本周开始时间
   public static LocalDateTime getCurrentWeekStartTime(LocalDateTime dateTime) {
      return dateTime.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).withHour(0).withMinute(0).withSecond(0).withNano(0);
   }
   //获取当前时间的本周结束时间
   public static LocalDateTime getCurrentWeekEndTime(LocalDateTime dateTime) {
      return dateTime.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)).withHour(23).withMinute(59).withSecond(59).withNano(999999999);
   }

   //获取当前时间的本月开始时间
   public static LocalDateTime getCurrentMonthStartTime(LocalDateTime dateTime) {
      return dateTime.withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
   }

   //获取当前时间的本月结束时间
   public static LocalDateTime getCurrentMonthEndTime(LocalDateTime dateTime) {
      return dateTime.with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59).withNano(999999999);
   }

   //当前时间的本季度开始时间,要求1-3月为第一季度,4-6月为第二季度,7-9月为第三季度,10-12为第四季度
    public static LocalDateTime getCurrentQuarterStartTime(LocalDateTime dateTime) {
        int month = dateTime.getMonthValue();
        int quarter = (month - 1) / 3;
        return dateTime.withMonth((quarter * 3) + 1).withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
    }

    //当前时间的本年开始时间
    public static LocalDateTime getCurrentYearStartTime(LocalDateTime dateTime) {
        return dateTime.withDayOfMonth(1).withMonth(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
    }

    //当前时间的本年结束时间
    public static LocalDateTime getCurrentYearEndTime(LocalDateTime dateTime) {
        return dateTime.withDayOfMonth(1).withMonth(1).with(TemporalAdjusters.lastDayOfYear()).withHour(23).withMinute(59).withSecond(59).withNano(999999999);
    }

    //当前时间的本季度结束时间,要求1-3月为第一季度,4-6月为第二季度,7-9月为第三季度,10-12为第四季度
    public static LocalDateTime getCurrentQuarterEndTime(LocalDateTime dateTime) {
        int month = dateTime.getMonthValue();
        int quarter = (month - 1) / 3;
        return dateTime.withMonth((quarter * 3) + 3).with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59).withNano(999999999);
    }

   //将当前日期按照指定格式转换成字符串
    public static String format(LocalDate date, String pattern) {
      return date.format(DateTimeFormatter.ofPattern(pattern));
   }


   //将当前时间转换成字符串 "yyyy-MM-dd HH:mm:ss"
    public static String formatDateTime(LocalDateTime date, String pattern) {
        return date.format(DateTimeFormatter.ofPattern(pattern));
    }

    //计算两个日期的间隔天数
    public static long getDaysBetween(LocalDateTime startDate, LocalDateTime endDate) {
        // 将两个日期调整为同一天的起始时间,然后计算天数差异
        ZonedDateTime startZonedDateTime = startDate.atZone(ZoneId.systemDefault());
        ZonedDateTime endZonedDateTime = endDate.atZone(ZoneId.systemDefault());
        // 计算两个 ZonedDateTime 之间的天数差异
        return ChronoUnit.DAYS.between(startZonedDateTime, endZonedDateTime)+1;
    }

}


二、使用案例

代码如下(示例):

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


public class DateUtils {
    public static void main(String[] args) {

        String dateString = "2024-03-31";//2024-02-29   2023-02-28
        LocalDate date = LocalDate.parse(dateString);

        LocalDate yesterday = DateTimeUtils.getYesterday(date);
        System.out.println("昨天日期:"+yesterday);

        LocalDate tomorrow = DateTimeUtils.getTomorrow(date);
        System.out.println("明天日期:"+tomorrow);

        LocalDate lastWeek = DateTimeUtils.getLastWeek(date);
        System.out.println("上周日期:"+lastWeek);

        LocalDate nextWeek = DateTimeUtils.getNextWeek(date);
        System.out.println("下周日期:"+nextWeek);

        LocalDate lastMonth = DateTimeUtils.getLastMonth(date);
        System.out.println("上个月日期:"+lastMonth);

        LocalDate nextMonth = DateTimeUtils.getNextMonth(date);
        System.out.println("下个月日期:"+nextMonth);

        LocalDate getLastYear = DateTimeUtils.getLastYear(date);
        System.out.println("去年日期:"+getLastYear);

        LocalDate nextYear = DateTimeUtils.getNextYear(date);
        System.out.println("明年日期:"+nextYear);

        LocalDate currentMonthStart = DateTimeUtils.getCurrentMonthStart(date);
        System.out.println("本月开始日期:"+currentMonthStart);

        LocalDate currentMonthEnd = DateTimeUtils.getCurrentMonthEnd(date);
        System.out.println("本月结束日期:"+currentMonthEnd);

        LocalDate currentQuarterStart = DateTimeUtils.getCurrentQuarterStart(date);
        System.out.println("本季度开始日期:"+currentQuarterStart);

        LocalDate currentQuarterEnd = DateTimeUtils.getCurrentQuarterEnd(date);
        System.out.println("本季度结束日期:"+currentQuarterEnd);

        LocalDate year = DateTimeUtils.getYear(date);
        System.out.println("当前年:"+year);

        LocalDate month = DateTimeUtils.getMonth(date);
        System.out.println("当前月:"+month);

        LocalDate day = DateTimeUtils.getDay(date);
        System.out.println("当前日:"+day);

        DayOfWeek currentWeek = DateTimeUtils.getCurrentWeek(date);
        System.out.println("当前星期:"+currentWeek);

        int currentQuarter = DateTimeUtils.getQuarter(date);
        System.out.println("当前季度:"+currentQuarter);


        System.out.println("==================================优雅的分割线================================================");
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dateTimeFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println(now);
        System.out.println("当前时间点:"+now.format(dateTimeFormat));

        LocalDateTime yesterdayLocalDateTime = DateTimeUtils.getYesterdayTime(now);
        System.out.println("昨天-当前时间点:"+yesterdayLocalDateTime.format(dateTimeFormat));

        LocalDateTime tomorrowLocalDateTime = DateTimeUtils.getTomorrowTime(now);
        System.out.println("明天-当前时间点:"+tomorrowLocalDateTime.format(dateTimeFormat));

        LocalDateTime currentDayStartTime = DateTimeUtils.getTodayStartTime(now);
        System.out.println("今天起始时间:"+currentDayStartTime.format(dateTimeFormat));

        LocalDateTime currentDayEndTime = DateTimeUtils.getTodayEndTime(now);
        System.out.println("今天结束时间:"+currentDayEndTime.format(dateTimeFormat));

        LocalDateTime currentWeekStartTime = DateTimeUtils.getCurrentWeekStartTime(now);
        System.out.println("本周起始时间:"+currentWeekStartTime.format(dateTimeFormat));

        LocalDateTime currentWeekEndTime = DateTimeUtils.getCurrentWeekEndTime(now);
        System.out.println("本周结束时间:"+currentWeekEndTime.format(dateTimeFormat));

        LocalDateTime currentMonthStartTime = DateTimeUtils.getCurrentMonthStartTime(now);
        System.out.println("本月起始时间:"+currentMonthStartTime.format(dateTimeFormat));

        LocalDateTime currentMonthEndTime = DateTimeUtils.getCurrentMonthEndTime(now);
        System.out.println("本月结束时间:"+currentMonthEndTime.format(dateTimeFormat));

        LocalDateTime currentQuarterStartTime = DateTimeUtils.getCurrentQuarterStartTime(now);
        System.out.println("本季度起始时间:"+currentQuarterStartTime.format(dateTimeFormat));

        LocalDateTime currentQuarterEndTime = DateTimeUtils.getCurrentQuarterEndTime(now);
        System.out.println("本季度结束时间:"+currentQuarterEndTime.format(dateTimeFormat));

        LocalDateTime currentYearStartTime = DateTimeUtils.getCurrentYearStartTime(now);
        System.out.println("本年起始时间:"+currentYearStartTime.format(dateTimeFormat));

        LocalDateTime currentYearEndTime = DateTimeUtils.getCurrentYearEndTime(now);
        System.out.println("本年结束时间:"+currentYearEndTime.format(dateTimeFormat));


        LocalDate localDate = LocalDate.of(2024, 02, 29);
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(DateTimeUtils.format(localDate,"yyyy-MM-dd"));
        System.out.println(DateTimeUtils.formatDateTime(localDateTime,"yyyy-MM-dd HH:mm:ss"));


        System.out.println("=========================================华丽的分割线=====================================================");
        //计算两个时间的间隔
        long daysBetween = DateTimeUtils.getDaysBetween(currentQuarterStartTime,LocalDateTime.now());
        System.out.println("当前时间与本年起始时间间隔:"+daysBetween);
    }
}

三、输出结果:

昨天日期:2024-03-30
明天日期:2024-04-01
上周日期:2024-03-24
下周日期:2024-04-07
上个月日期:2024-02-29
下个月日期:2024-04-30
去年日期:2023-03-31
明年日期:2025-03-31
本月开始日期:2024-03-01
本月结束日期:2024-03-31
本季度开始日期:2024-01-01
本季度结束日期:2024-03-31
当前年:2024-01-01
当前月:2024-03-01
当前日:2024-03-31
当前星期:SUNDAY
当前季度:1
==================================优雅的分割线================================================
2024-04-15T15:27:00.697563300
当前时间点:2024-04-15 15:27:00
昨天-当前时间点:2024-04-14 15:27:00
明天-当前时间点:2024-04-16 15:27:00
今天起始时间:2024-04-15 00:00:00
今天结束时间:2024-04-15 23:59:59
本周起始时间:2024-04-15 00:00:00
本周结束时间:2024-04-21 23:59:59
本月起始时间:2024-04-01 00:00:00
本月结束时间:2024-04-30 23:59:59
本季度起始时间:2024-04-01 00:00:00
本季度结束时间:2024-06-30 23:59:59
本年起始时间:2024-01-01 00:00:00
本年结束时间:2024-12-31 23:59:59
2024-02-29
2024-04-15 15:27:00
=========================================华丽的分割线=====================================================
当前时间与本年起始时间间隔:15

Process finished with exit code 0


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值