Javaは、指定された日付からの経過時間を実装します。

 public static void main(String[] args) {
    
    
        String str = "2022-07-05 09:00:00";
        System.err.println(getPastTimes(str));
    }

    public static String getPastTimes(String date) {
    
    
        // 指定格式时间转换
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
    
    
            // 转换成日期
            Date dte = simpleDateFormat.parse(date);
            // 时间转换成毫秒值
            long datetime = dte.getTime();
            // 获取当前日期毫秒值
            long nowDate = new Date().getTime();
            // 差值
            long miss = nowDate - datetime;
            // 毫秒值处理
            long days = miss / (1000 * 60 * 60 * 24);
            long hours = (miss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
            long minutes = (miss % (1000 * 60 * 60)) / (1000 * 60);
            long seconds = (miss % (1000 * 60)) / 1000;
            return days + " 天 " + hours + " 时 " + minutes + " 分 " + seconds + " 秒 ";
        } catch (Exception e) {
    
    
            e.printStackTrace();
            return "";
        }
    }

ここに画像の説明を挿入します

おすすめ

転載: blog.csdn.net/qq_45752401/article/details/125644679