时间工具类使用总结(持续更新中)

package com.yf.test;

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

/*********************************************************
 **
 ** 

** @author: yangfeng ** @date: 2023/10/30 11:40 ** @version: 1.0.0 *********************************************************/
public class DateTimeUtils { public static void main(String[] args) { // // 获取当天日期 // LocalDate now = LocalDate.now(); // // // 当天开始时间 // LocalDateTime todayStart = now.atStartOfDay(); // // 当天结束时间 // LocalDateTime todayEnd = LocalDateTime.of(now, LocalTime.MAX); // // // 周一 // LocalDate monday = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)); // // 周日 // LocalDate sunday = now.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)); // // // 本周开始时间 // LocalDateTime weekStart = monday.atStartOfDay(); // // 本周结束时间 // LocalDateTime weekEnd = LocalDateTime.of(sunday, LocalTime.MAX); // // // 本月1号 // LocalDate firstDayOfMonth = now.with(TemporalAdjusters.firstDayOfMonth()); // // 本月最后一天 // LocalDate lastDayOfMonth = now.with(TemporalAdjusters.lastDayOfMonth()); // // // 本月1号的开始时间 // LocalDateTime firstDayOfMonthStart = firstDayOfMonth.atStartOfDay(); // // 本月最后一天的最后时间 // LocalDateTime firstDayOfMonthEnd = LocalDateTime.of(lastDayOfMonth, LocalTime.MAX); // // // 今年第一天 // LocalDate firstDayOfYear = LocalDate.now().with(TemporalAdjusters.firstDayOfYear()); // // 今年最后一天 // LocalDate lastDayOfYear = LocalDate.now().with(TemporalAdjusters.lastDayOfYear()); // // // 本年度1号的开始时间 // LocalDateTime firstDayOfYearStart = firstDayOfYear.atStartOfDay(); // // 本年度最后时间 // LocalDateTime firstDayOfYearEnd = LocalDateTime.of(lastDayOfYear, LocalTime.MAX); // // //获取前一天日期 // LocalDate yesterday2 = LocalDate.now().minusDays(1); DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); System.out.println("当天开始时间 = " + getCurrentStartTime().format(pattern) + " 时间戳 :" + getCurrentStartTime().toInstant(ZoneOffset.of("+8")).toEpochMilli()); System.out.println("当天结束时间 = " + getCurrentEndTime().format(pattern) + " 时间戳 :" + getCurrentEndTime().toInstant(ZoneOffset.of("+8")).toEpochMilli()); System.out.println("本周开始时间 = " + getWeekStartTime().format(pattern) + " 时间戳 :" + getWeekStartTime().toInstant(ZoneOffset.of("+8")).toEpochMilli()); System.out.println("本周结束时间 = " + getWeekEndTime().format(pattern) + " 时间戳 :" + getWeekEndTime().toInstant(ZoneOffset.of("+8")).toEpochMilli()); System.out.println("本月开始时间 = " + getMonthStartTime().format(pattern) + " 时间戳 :" + getMonthStartTime().toInstant(ZoneOffset.of("+8")).toEpochMilli()); System.out.println("本月结束时间 = " + getMonthEndTime().format(pattern) + " 时间戳 :" + getMonthEndTime().toInstant(ZoneOffset.of("+8")).toEpochMilli()); System.out.println("本季度开始时间 = " + getQuarterStartTime().format(pattern) + " 时间戳 :" + getQuarterStartTime().toInstant(ZoneOffset.of("+8")).toEpochMilli()); System.out.println("本季度结束时间 = " + getQuarterEndTime().format(pattern) + " 时间戳 :" + getQuarterEndTime().toInstant(ZoneOffset.of("+8")).toEpochMilli()); System.out.println("本年开始时间 = " + getYearStartTime().format(pattern) + " 时间戳 :" + getYearStartTime().toInstant(ZoneOffset.of("+8")).toEpochMilli()); System.out.println("本年结束时间 = " + getYearEndTime().format(pattern) + " 时间戳 :" + getYearEndTime().toInstant(ZoneOffset.of("+8")).toEpochMilli()); System.out.println(getStartOrEndDayOfQuarter(LocalDate.now(), false)); } public static String getStartOrEndDayOfQuarter(LocalDate today, Boolean isFirst){ LocalDate resDate = LocalDate.now(); if (today == null) { today = resDate; } Month month = today.getMonth(); Month firstMonthOfQuarter = month.firstMonthOfQuarter(); Month endMonthOfQuarter = Month.of(firstMonthOfQuarter.getValue() + 2); if (isFirst) { resDate = LocalDate.of(today.getYear(), firstMonthOfQuarter, 1); } else { resDate = LocalDate.of(today.getYear(), endMonthOfQuarter, endMonthOfQuarter.length(today.isLeapYear())); } return resDate.toString(); } /** * 获取当天开始时间 * @Param [] * @return java.time.LocalDateTime * @Date 20:49 2023/12/9 * @Author yangfeng **/ public static LocalDateTime getCurrentStartTime(){ LocalDate now = LocalDate.now(); // 当天开始时间 LocalDateTime todayStart = now.atStartOfDay(); return todayStart; } /** * 获取当天结束时间 * @Param [] * @return java.time.LocalDateTime * @Date 20:51 2023/12/9 * @Author yangfeng **/ public static LocalDateTime getCurrentEndTime(){ LocalDate now = LocalDate.now(); // 当天结束时间 LocalDateTime todayEnd = LocalDateTime.of(now, LocalTime.MAX); return todayEnd; } /** * 获取本周开始时间 * @Param [] * @return java.time.LocalDateTime * @Date 20:55 2023/12/9 * @Author yangfeng **/ public static LocalDateTime getWeekStartTime(){ LocalDate now = LocalDate.now(); // 周一 LocalDate monday = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)); // 本周开始时间 LocalDateTime weekStart = monday.atStartOfDay(); return weekStart; } /** * 获取本周结束时间 * @Param [] * @return java.time.LocalDateTime * @Date 20:57 2023/12/9 * @Author yangfeng **/ public static LocalDateTime getWeekEndTime(){ LocalDate now = LocalDate.now(); // 周日 LocalDate sunday = now.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)); // 本周结束时间 LocalDateTime weekEnd = LocalDateTime.of(sunday, LocalTime.MAX); return weekEnd; } /** * 获取本月开始时间 * @Param [] * @return java.time.LocalDateTime * @Date 21:00 2023/12/9 * @Author yangfeng **/ public static LocalDateTime getMonthStartTime(){ LocalDate now = LocalDate.now(); // 本月1号 LocalDate firstDayOfMonth = now.with(TemporalAdjusters.firstDayOfMonth()); // 本月1号的开始时间 LocalDateTime firstDayOfMonthStart = firstDayOfMonth.atStartOfDay(); return firstDayOfMonthStart; } /** * 获取本月最后一天 * @Param [] * @return java.time.LocalDateTime * @Date 21:02 2023/12/9 * @Author yangfeng **/ public static LocalDateTime getMonthEndTime(){ LocalDate now = LocalDate.now(); // 本月最后一天 LocalDate lastDayOfMonth = now.with(TemporalAdjusters.lastDayOfMonth()); // 本月最后一天的最后时间 LocalDateTime firstDayOfMonthEnd = LocalDateTime.of(lastDayOfMonth, LocalTime.MAX); return firstDayOfMonthEnd; } /** * 获取本季度开始时间 * @Param [] * @return java.time.LocalDateTime * @Date 21:11 2023/12/9 * @Author yangfeng **/ public static LocalDateTime getQuarterStartTime(){ // 获取当天日期 LocalDate now = LocalDate.now(); Month month = now.getMonth(); Month firstMonthOfQuarter = month.firstMonthOfQuarter(); LocalDateTime quarterStartTime = LocalDateTime.of(now.getYear(), firstMonthOfQuarter, 1, 0, 0, 0); return quarterStartTime; } /** * 获取本季度结束时间 * @Param [] * @return java.time.LocalDateTime * @Date 21:15 2023/12/9 * @Author yangfeng **/ public static LocalDateTime getQuarterEndTime(){ // 获取当天日期 LocalDate now = LocalDate.now(); Month month = now.getMonth(); Month firstMonthOfQuarter = month.firstMonthOfQuarter(); Month endMonthOfQuarter = Month.of(firstMonthOfQuarter.getValue() + 2); LocalDateTime quarterEndTime = LocalDateTime.of(now.getYear(), endMonthOfQuarter, endMonthOfQuarter.length(now.isLeapYear()), 23, 59, 59); return quarterEndTime; } /** * 获取本年度开始时间 * @Param [] * @return java.time.LocalDateTime * @Date 21:16 2023/12/9 * @Author yangfeng **/ public static LocalDateTime getYearStartTime(){ // 今年第一天 LocalDate firstDayOfYear = LocalDate.now().with(TemporalAdjusters.firstDayOfYear()); // 本年度1号的开始时间 LocalDateTime firstDayOfYearStart = firstDayOfYear.atStartOfDay(); return firstDayOfYearStart; } /** * 获取本年度最后一天 * @Param [] * @return java.time.LocalDateTime * @Date 21:19 2023/12/9 * @Author yangfeng **/ public static LocalDateTime getYearEndTime(){ // 今年最后一天 LocalDate lastDayOfYear = LocalDate.now().with(TemporalAdjusters.lastDayOfYear()); // 本年度最后时间 LocalDateTime firstDayOfYearEnd = LocalDateTime.of(lastDayOfYear, LocalTime.MAX); return firstDayOfYearEnd; } /** * 获取两个时间节点相差的毫秒数 * @Param [start, end] * @return long * @Date 21:32 2023/12/9 * @Author yangfeng **/ public static long getTwoTimeDifferMillis(LocalDateTime start,LocalDateTime end){ return Duration.between(start, end).toMillis(); } }

你可能感兴趣的:(java,servlet,开发语言)