帮我写一个传入两个时间字符串的Java方法,实现这两个时间的差 Java8
时间: 2024-05-31 10:07:18 浏览: 106
import java.time.Duration;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class TimeDifference {
public static void main(String[] args) {
String time1 = "10:30:00";
String time2 = "11:45:30";
Duration diff = getTimeDifference(time1, time2);
System.out.println("Time difference is " + diff);
}
public static Duration getTimeDifference(String time1, String time2) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
LocalTime localTime1 = LocalTime.parse(time1, formatter);
LocalTime localTime2 = LocalTime.parse(time2, formatter);
return Duration.between(localTime1, localTime2);
}
}
阅读全文