java计算两个时间年月日时分秒差值工具类

前言

该工具类为自己项目中所写,可以拿来即用,若工具类名称与项目中工具类名称冲突,可自行修改,该工具类主要用于解决计算两个LocalDataTime的时间差值,并直接返回时间差对应的年月日时分秒。

工具类

package com.cdqckj.gmis.common.utils;

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

/**
 * LocalDateTime时间操作工具类,可自行扩展方法
 *
 * @author wyj
 * @date 2021/11/5 10:28
 */
public class LocalDateTimeUtil {

  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 start 开始时间
   * @param end 结束时间
   * @return 时间差值
   * @date 2021/11/5 10:30
   */
  public static String getTimeTnterval(String start, String end) {
    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 时间差值
   * @date 2021/11/5 10:30
   */
  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("年");
    }
    long month = tempDateTime.until(end, ChronoUnit.MONTHS);
    if (month != 0) {
      builder.append(month).append("月");
    }
    tempDateTime = tempDateTime.plusMonths(month);
    long days = tempDateTime.until(end, ChronoUnit.DAYS);
    if (days != 0) {
      builder.append(days).append("天");
    }
    tempDateTime = tempDateTime.plusDays(days);
    long hours = tempDateTime.until(end, ChronoUnit.HOURS);
    if (hours != 0) {
      builder.append(hours).append("小时");
    }
    tempDateTime = tempDateTime.plusHours(hours);
    long minutes = tempDateTime.until(end, ChronoUnit.MINUTES);
    if (minutes != 0) {
      builder.append(minutes).append("分钟");
    }
    tempDateTime = tempDateTime.plusMinutes(minutes);
    long seconds = tempDateTime.until(end, ChronoUnit.SECONDS);
    if (seconds != 0) {
      builder.append(seconds).append("秒");
    }
    return builder.toString();
  }

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值