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

2022-06-21 00:00:00 相差 时分 年月日

这里是计算两个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秒
    原文作者:java_代码搬运工
    原文地址: https://blog.csdn.net/qq_38164123/article/details/108150956
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。

相关文章