计算两个日期的时间差,年月日时分秒

package com.lrhealth.mappingintegration.utils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Objects;

/**
 * ClassName: TimeUtil
 * Description:计算时间差
 *
 * @author pangyq
 * date: 2022/2/7 15:45
 */
@Component
public class TimeUtil {



    private static final String LOCAL_DATATIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    /**
     * 计算两个时间差值并转换为时分秒 例如: 2021-11-05 09:53:05和2021-11-05 09:53:08 返回结果为3秒
     *
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 时间差值
     */
    public static String getTimeTnterval(Date startTime, Date endTime) {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String start = formatter.format(startTime);
        String end = formatter.format(endTime);
        if (Objects.isNull(start) || Objects.isNull(end)) {
            return null;
        }
        return getTimeTnterval(parseStringToDateTime(start), parseStringToDateTime(end));
    }

    /**
     * 计算两个时间差值并转换为时分秒 例如: 2021-11-05 09:53:05和2021-11-05 09:53:08 返回结果为3秒
     *
     * @param start 开始时间
     * @param end 结束时间
     * @return 时间差值
     */
    public static String getTimeTnterval(LocalDateTime start, LocalDateTime end) {
        if (Objects.isNull(start) || Objects.isNull(end)) {
            return null;
        }
        StringBuilder builder = new StringBuilder();
        LocalDateTime tempDateTime = LocalDateTime.from(start);
        long year = tempDateTime.until(end, ChronoUnit.YEARS);
        if (year != 0) {
            builder.append(year).append("year");
        }
        long month = tempDateTime.until(end, ChronoUnit.MONTHS);
        if (month != 0) {
            builder.append(month).append("month");
        }
        tempDateTime = tempDateTime.plusMonths(month);
        long days = tempDateTime.until(end, ChronoUnit.DAYS);
        if (days != 0) {
            builder.append(days).append("day");
        }
        tempDateTime = tempDateTime.plusDays(days);
        long hours = tempDateTime.until(end, ChronoUnit.HOURS);
        if (hours != 0) {
            builder.append(hours).append("h");
        }
        tempDateTime = tempDateTime.plusHours(hours);
        long minutes = tempDateTime.until(end, ChronoUnit.MINUTES);
        if (minutes != 0) {
            builder.append(minutes).append("min");
        }
        tempDateTime = tempDateTime.plusMinutes(minutes);
        long seconds = tempDateTime.until(end, ChronoUnit.SECONDS);
        if (seconds != 0) {
            builder.append(seconds).append("s");
        }
        if (StringUtils.isEmpty(builder.toString())){
            builder.append("0s");
        }
        return builder.toString();
    }

    /**
     * 将指定时间字符串转换为LocalDateTime
     *
     * @param time 待转换时间字符串
     * @return 转换结果
     */
    public static LocalDateTime parseStringToDateTime(String time) {
        return LocalDateTime.parse(time, DateTimeFormatter.ofPattern(LOCAL_DATATIME_FORMAT));
    }


    }

  

posted @ 2022-02-09 14:38  小小菜包子  阅读(332)  评论(0)    收藏  举报