-1

我一天有 2 个小时,当我减去时间时,我只得到小时值,小时和分钟是长数据类型。如何获得小时和分钟?

例如:今天 10:10 - 明天 7:45 = 21 小时 35 分钟

4

1 回答 1

1

您可以使用 SimpleDateFormat 类提供完整的日期信息:

DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date startDate = df.parse("2016-05-24 17:50:10");
        Date endDate = df.parse("2016-05-25 19:05:15");
        long diff = endDate.getTime() - startDate.getTime();

        int timeInSeconds = (int) (diff / 1000);
        int hours, minutes, seconds;
        hours = timeInSeconds / 3600;
        timeInSeconds = timeInSeconds - (hours * 3600);
        minutes = timeInSeconds / 60;
        timeInSeconds = timeInSeconds - (minutes * 60);
        seconds = timeInSeconds;

        System.out.println(String.format("%d:%d':%d\"", hours, minutes, seconds));
于 2016-05-26T12:27:02.077 回答