java两个时间段计算相差时长

计算两个时间段相差时长

复制代码
public static String getDistanceTime(Long  str1 ,Long str2) {
        long day = 0;//天数差
        long hour = 0;//小时数差
        long min = 0;//分钟数差
        long second=0;//秒数差
        long diff=0 ;//毫秒差
        StringBuilder result =new StringBuilder();
        //String result = "";
        diff = str2-str1;
        day = diff / (24 * 60 * 60 * 1000);
        hour = (diff / (60 * 60 * 1000) - day * 24);
        min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
        second = diff/1000;
        System.out.println("day="+day+" hour="+hour+" min="+min+" ss="+second%60+" SSS="+diff%1000);
        String daystr = day%30+"天";
        String hourStr = hour%24+"小时";
        String minStr = min%60+"分";
        String secondStr = second%60+"秒";
        if (day!=0){
            result.append(daystr);
            //result = result + daystr;
        }
        if (hour!=0){
            result.append(hourStr);
            //result = result + hourStr;
        }
        if (min!=0){
            result.append(minStr);
            //result = result + minStr;
        }
        if (second!=0){
            result.append(secondStr);
            //result = result + secondStr;
        }
        return result.toString();
    }

创建主方法

复制代码
public static void main(String[] args) throws ParseException {
        String a="2023-07-27 14:43:00";
        String b="2023-07-28 14:30:10";
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse1 = dateFormat.parse(a);
        Date parse2 = dateFormat.parse(b);
        String bigDecimal=getDistanceTime(parse1.getTime(),parse2.getTime());
        System.out.println(bigDecimal);
      
    }

运行结果

相关推荐
没有梦想的咸鱼185-1037-16635 分钟前
【大语言模型DeepSeek+ChatGPT+GIS+Python】AI大语言模型驱动的地质灾害全流程智能防治:风险评估、易发性分析与灾后重建多技术融合应用
人工智能·python·机器学习·arcgis·语言模型·chatgpt·数据分析
Luck_ff08105 分钟前
【Python爬虫详解】第五篇:使用正则表达式提取网页数据
爬虫·python·正则表达式
RAVEN_14527 分钟前
python精度问题,两个数不等但相减为零
python
我的golang之路果然有问题15 分钟前
案例速成GO+Socket,个人笔记
开发语言·笔记·后端·websocket·学习·http·golang
roc-ever15 分钟前
用Python做有趣的AI项目 6:AI音乐生成器(LSTM Melody Generator)
人工智能·python·lstm
我的golang之路果然有问题16 分钟前
快速了解Go+rpc
开发语言·经验分享·笔记·rpc·golang·go
xiaohanbao0922 分钟前
day10 python机器学习全流程实践
人工智能·python·学习·机器学习·信息可视化·pandas
一眼青苔32 分钟前
python中 str.strip() 是什么意思
开发语言·python
heyCHEEMS35 分钟前
[USACO09OCT] Bessie‘s Weight Problem G Java
java·开发语言·算法
阿伟来咯~1 小时前
vue3+Nest.js项目 部署阿里云
开发语言·javascript·ecmascript