java计算两个时间差多少小时 多少分钟

    public static void main(String[] args) throws ParseException {
        Date date = new Date();//当前时间
        String otherTime = "2021-11-05 16:00:00";
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date time = sf.parse(otherTime);
//      当前时间与其他时间相差的毫秒数
        long diff = date.getTime() - time.getTime();
        long sec = diff / 1000;
        long min = diff / 60 / 1000;
        long hours = diff / 60 / 60 / 1000;
        System.out.println("相差多少秒" + sec);
        System.out.println("相差多少分钟" + min);
        System.out.println("相差多少小时" + hours);

    }

你可能感兴趣的:(JAVA基础,java,开发语言,后端)