JavaSE-时间差

2022/7/28 1:24:02

本文主要是介绍JavaSE-时间差,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

public static String getDiffDateStr(Date startTime,Date endTime){
        long diff = endTime.getTime() - startTime.getTime();

        long diffSeconds = diff / 1000 % 60;

        long diffMinutes = diff / (60 * 1000) % 60;

        long diffHours = diff / (60 * 60 * 1000) % 24;

        long diffDays = diff / (24 * 60 * 60 * 1000);

        String diffTimeStr = "";
        if (0 != diffDays) {
            diffTimeStr = diffTimeStr + diffDays + "天";
        }
        if (0 != diffHours) {
            diffTimeStr = diffTimeStr + diffHours + "小时";
        }
        if (0 != diffMinutes) {
            diffTimeStr = diffTimeStr + diffMinutes + "分钟";
        }
        if (0 != diffSeconds) {
            diffTimeStr = diffTimeStr + diffSeconds + "秒";
        }

        return diffTimeStr;

    }


这篇关于JavaSE-时间差的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程