package com.fastbee.common.utils.date;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.time.format.DateTimeFormatter;

public class DateTimeCalculationUtils {
    // 获取当前时间并减去指定单位的指定数量
    public static LocalDateTime minus(long amountToSubtract, ChronoUnit unit) {
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 减去指定的时间量
        return now.minus(amountToSubtract, unit);
    }

    // 格式化日期时间
    public static String format(LocalDateTime dateTime) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return dateTime.format(formatter);
    }

    public static void main(String[] args) {
        System.out.println("DateTime before 5 minutes: " + DateTimeCalculationUtils.format(DateTimeCalculationUtils.minus(5, ChronoUnit.MINUTES)));
    }
}