Java 计算两个日期相差年月日时分秒

这里是计算两个LocalDateTime时间差的方法。

这里列举两种时差计算

方法一和方法二计算结果差别,在于是否将上一个时差执行结果同步到下一个时差计算当中。

方法一:计算两个日期的总计相差时间

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;

public class DateTest {

    public static void main(String[] args) {
        Date date = new Date();
        date.setYear(119);
        date.setMonth(5);
        date.setDate(23);
        String s = calculateTimeDifference(date, new Date());
        System.out.println(s);

    }

    /**
     * 计算两个时间差(年,月,星期,日,时,分,秒)
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static String calculateTimeDifference(Date startDate, Date endDate) {
        if (null == startDate || null == endDate) {
            return "";
        }
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime fromDateTime = LocalDateTime.ofInstant(startDate.toInstant(), zoneId);
        LocalDateTime toDateTime = LocalDateTime.ofInstant(endDate.toInstant(), zoneId);

        LocalDateTime tempDateTime = LocalDateTime.from(fromDateTime);

        long years = tempDateTime.until(toDateTime, ChronoUnit.YEARS);
        tempDateTime = tempDateTime.plusYears(years);

        long months = tempDateTime.until(toDateTime, ChronoUnit.MONTHS);
        tempDateTime = tempDateTime.plusMonths(months);

        long days = tempDateTime.until(toDateTime, ChronoUnit.DAYS);
        tempDateTime = tempDateTime.plusDays(days);

        long hours = tempDateTime.until(toDateTime, ChronoUnit.HOURS);
        tempDateTime = tempDateTime.plusHours(hours);

        long minutes = tempDateTime.until(toDateTime, ChronoUnit.MINUTES);
        tempDateTime = tempDateTime.plusMinutes(minutes);

        long seconds = tempDateTime.until(toDateTime, ChronoUnit.SECONDS);


        return (0 == years ? "" : years + "年")
                + (0 == months ? "" : months + "月")
                + (0 == days ? "" : days + "天") 
                + (hours + "小时")
                + (minutes + "小时")
                + (seconds + "秒");

    }
}
控制台输出如下格式(例如):1年1月29天0小时0小时0秒

方法二:计算两个日期的分别时间差(换算为年总计,换算为月的总计,换算为日的总计 等等... ....)

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;

public class DateTest {

    public static void main(String[] args) {
        Date date = new Date();
        date.setYear(119);
        date.setMonth(5);
        date.setDate(23);
        String s = calculateTimeDifference(date, new Date());
        System.out.println(s);

    }

    /**
     * 计算两个时间差(年,月,星期,日,时,分,秒)
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static String calculateTimeDifference(Date startDate, Date endDate) {
        if (null == startDate || null == endDate) {
            return "";
        }
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime fromDateTime = LocalDateTime.ofInstant(startDate.toInstant(), zoneId);
        LocalDateTime toDateTime = LocalDateTime.ofInstant(endDate.toInstant(), zoneId);

        LocalDateTime tempDateTime = LocalDateTime.from(fromDateTime);

        long years = tempDateTime.until(toDateTime, ChronoUnit.YEARS);

        long months = tempDateTime.until(toDateTime, ChronoUnit.MONTHS);

        long days = tempDateTime.until(toDateTime, ChronoUnit.DAYS);

        long hours = tempDateTime.until(toDateTime, ChronoUnit.HOURS);

        long minutes = tempDateTime.until(toDateTime, ChronoUnit.MINUTES);

        long seconds = tempDateTime.until(toDateTime, ChronoUnit.SECONDS);

        return (0 == years ? "" : years + "年")
                + (0 == months ? "" : months + "月")
                + (0 == days ? "" : days + "天")
                + (hours + "小时")
                + (minutes + "小时")
                + (seconds + "秒");

    }
}
控制台输出如下格式(例如):1年13月425天10200小时612000小时36720000秒
  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值