Java 时间差运算工具函数(时间戳运算)

本文介绍了一种在Java中计算两个日期之间的时间差的方法,并展示了如何将毫秒数转换为易于理解的时间格式。通过具体实例,演示了不同时间单位(秒、分钟、小时和天)的时间差计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

main 函数调用 :

 public static void main(String[] args) throws ParseException {
    // 获取指定long型的时间
    System.out.println(parseMillisecone(436765000L));
    ;
    // 获取时间差的秒数
    long diff = getDifference(new Date(), new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss").parse("2016-12-10 00:00:00"), 0);
    System.out.println(getDifference(new Date(), new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss").parse("2016-12-10 00:00:00"), 0));
    System.out.println("时间:" + parseMillisecone(diff));
    System.out.println("时间:" + parseMillisecone(diff * 1000));
}

打印结果:

5天1时19分25秒
715051
时间:0天0时11分55秒
时间:8天6时37分31秒

方法:

/**
     * 两个时间间的时间戳计算函数
     * @param beginDate
     * @param endDate
     * @param f  时间差的形式0:秒,1:分种,2:小时,3:天
     * @return  long 秒
     */
    public static long getDifference(Date beginDate, Date endDate, int f) {
        long result = 0;
        if (beginDate == null || endDate == null) {
            return 0;
        }
        try {
            // 日期相减获取日期差X(单位:毫秒)
            long millisecond = endDate.getTime() - beginDate.getTime();
            /**
             * Math.abs((int)(millisecond/1000)); 绝对值 1秒 = 1000毫秒
             * millisecond/1000 --> 秒 millisecond/1000*60 - > 分钟
             * millisecond/(1000*60*60) -- > 小时 millisecond/(1000*60*60*24) -->
             * 天
             * */
            switch (f) {
            case 0: // second
                return  (millisecond / 1000);
            case 1: // minute
                return (millisecond / (1000 * 60));
            case 2: // hour
                return  (millisecond / (1000 * 60 * 60));
            case 3: // day
                return (millisecond / (1000 * 60 * 60 * 24));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }



 /**
 * 计算时差 根据 long 返回时间点
 * 
 * @param millisecond
 * @return string 0天0时11分55秒
 */
public static String parseMillisecone(long millisecond) {
    String time = null;
    try {
        long yushu_day = millisecond % (1000 * 60 * 60 * 24);
        long yushu_hour = (millisecond % (1000 * 60 * 60 * 24))
                % (1000 * 60 * 60);
        long yushu_minute = millisecond % (1000 * 60 * 60 * 24)
                % (1000 * 60 * 60) % (1000 * 60);
        @SuppressWarnings("unused")
        long yushu_second = millisecond % (1000 * 60 * 60 * 24)
                % (1000 * 60 * 60) % (1000 * 60) % 1000;
        if (yushu_day == 0) {
            return (millisecond / (1000 * 60 * 60 * 24)) + "天";
        } else {
            if (yushu_hour == 0) {
                return (millisecond / (1000 * 60 * 60 * 24)) + "天"
                        + (yushu_day / (1000 * 60 * 60)) + "时";
            } else {
                if (yushu_minute == 0) {
                    return (millisecond / (1000 * 60 * 60 * 24)) + "天"
                            + (yushu_day / (1000 * 60 * 60)) + "时"
                            + (yushu_hour / (1000 * 60)) + "分";
                } else {
                    return (millisecond / (1000 * 60 * 60 * 24)) + "天"
                            + (yushu_day / (1000 * 60 * 60)) + "时"
                            + (yushu_hour / (1000 * 60)) + "分"
                            + (yushu_minute / 1000) + "秒";

                }

            }

        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return time;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Arisono

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值