工作中用到的,记录一下
1、常用时间
package com.zhxd.caochang.qingyan.utils;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
/**
* @Author: hzj
* @Date: 2022/07/19/14:01
*/
public class LocalDateTimeUtils {
public static LocalDateTime now = LocalDateTime.now();
public static LocalDate today = LocalDate.now();
public static LocalDateTime todayBegin = LocalDateTime.of(today, LocalTime.MIN);
public static LocalDateTime todayEnd = LocalDateTime.of(today, LocalTime.MAX);
public static LocalDate lastDay = today.minusDays(1);
public static LocalDateTime lastDayBegin = todayBegin.minusDays(1);
public static LocalDateTime lastDayEnd = todayEnd.minusDays(1);
public static LocalDateTime last2DayEnd = todayEnd.minusDays(2);
public static LocalDate lastYearDay = lastDay.minusYears(1);
public static LocalDateTime lastYearDayBegin = todayBegin.minusYears(1);
public static LocalDateTime lastYearDayEnd = todayEnd.minusYears(1);
public static LocalDateTime lastYearLastDayEnd = lastYearDayEnd.minusDays(1);
public static LocalDateTime lastYearNextDayBegin = lastYearDayBegin.plusDays(1);
public static LocalDateTime lastYear2DayBegin = lastYearDayBegin.minusDays(1);
public static LocalDate nextDay = lastDay.plusDays(1);
public static LocalDateTime nextDayBegin = todayBegin.plusDays(1);
public static LocalDateTime nextDayEnd = todayEnd.plusDays(1);
public static LocalDate currentMonth = LocalDate.now();
public static LocalDateTime currentMonthBegin = now.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN);
public static LocalDateTime currentMonthEnd = now.with(TemporalAdjusters.lastDayOfMonth()).with(LocalTime.MAX);
public static LocalDateTime nextMonthBegin = currentMonthBegin.plusDays(1);
public static LocalDate lastMonthDay = currentMonth.minusMonths(1);
public static String lastMonthStr = currentMonth.minusMonths(1).format(DateTimeFormatter.ofPattern("yyyy-MM"));
public static LocalDateTime lastMonthBegin = currentMonthBegin.minusMonths(1);
public static LocalDateTime lastMonthEnd = currentMonthEnd.minusMonths(1);
public static LocalDateTime lastMonth2Begin = lastMonthBegin.plusMonths(1);
public static LocalDateTime lastMonth2End = currentMonthEnd.minusMonths(2);
public static int lastYearValue = today.getYear() - 1;
public static LocalDateTime lastYearMonthBegin = currentMonthBegin.minusYears(1);
public static LocalDateTime lastYearMonthEnd = currentMonthEnd.minusYears(1);
public static LocalDateTime lastYearLastMonthEnd = lastYearMonthEnd.minusMonths(1);
public static LocalDateTime lastYearNextMonthBegin = lastYearMonthBegin.plusMonths(1);
public static LocalDateTime currentYearBegin = now.with(TemporalAdjusters.firstDayOfYear()).with(LocalTime.MIN);
public static LocalDateTime currentYearEnd = now.with(TemporalAdjusters.lastDayOfYear()).with(LocalTime.MAX);
public static LocalDateTime lastYearBegin = currentYearBegin.minusYears(1);
public static LocalDateTime lastYearEnd = currentYearEnd.minusYears(1);
public static LocalDateTime nextYearBegin = currentYearBegin.plusYears(1);
public static LocalDateTime last2YearEnd = lastYearEnd.minusYears(1);
}
2、带有T和Z的时间处理
public static void main(String[] args) {
String p = "2022-07-20T15:55:55.999Z";
LocalDateTime z = LocalDateTime.parse(p.replace("Z", "+0000"),
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
System.out.println(z);
}