我正在开发一个程序,它将返回两个时间点之间经过的时间差(多少小时,分钟)。如果时间是11:00 pm到3:00,我需要能够计算时间。我怎样才能这样做,以及从早上8点到下午5点计算时间?
谢谢!
答案 0 :(得分: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.");
}
答案 1 :(得分:0)
如果有人正在寻找解决方案,您可以像这样answer
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)));
}