-1

我正在开发一个程序,该程序将返回两个时间点之间经过的时间差(多少小时,分钟)。我还需要能够计算时间,如果时间是晚上 11:00 到凌晨 3:00。我将如何执行此操作,以及计算从上午 8:00 到下午 5:00 的时间?

谢谢!

4

2 回答 2

1
public static void main(String[] av) {
    /** The date at the end of the last century */
    Date d1 = new GregorianCalendar(2000, 11, 31, 23, 59).getTime();

    /** Today's date */
    Date today = new Date();

    // Get msec from each, and subtract.
    long diff = today.getTime() - d1.getTime();

    System.out.println("The 21st century (up to " + today + ") is "
        + (diff / (1000 * 60 * 60 * 24)) + " days old.");
}
于 2012-04-27T19:00:52.733 回答
1

如果有人正在寻找解决方案,您可以这样

String time = "22:00-01:05";
String[] parts = time.split("-");

LocalTime start = LocalTime.parse(parts[0]);
LocalTime end = LocalTime.parse(parts[1]);
if (start.isBefore(end)) { // normal case
    System.out.println(Duration.between(start, end));
} else { // 24 - duration between end and start, note how end and start switched places
    System.out.println(Duration.ofHours(24).minus(Duration.between(end, start)));
}
于 2018-12-10T19:09:57.520 回答