我正在尝试查找HH:MM:SS格式的两个LocalTime对象之间的时间差,而我的工期对象给了我错误工期。两者无法解析为类型。Java文档上的between描述明确表示LocalTime对象是有效的——我是否必须将我的对象转换为带有区域的LocalDateTime对象才能工作?
关于这个问题,唯一的其他理论是我把这个类放在我的Make文件中,但我认为我的导入会在编译过程中解决这个问题。以下代码:
import java.time.*;
import java.time.Duration;
public class Station {
int stationNumber;
String stationAddress;
ArrayList<Driver> drivers;
ArrayList<Road> roads;
double[][] roadMap;
public Station(int sNumber, String sAddress) {
stationNumber = sNumber;
stationAddress = sAddress;
drivers = new ArrayList<Driver>();
roads = new ArrayList<Road>(5);
roadMap = new double[5][5];
}
public static void calculateNewTimes(ArrayList<roadDataToBeSorted> delDataIncreasingTime) {
LocalTime firstStop = LocalTime.parse(delDataIncreasingTime.get(0).rdDeliveryTime);
LocalTime lastStop = LocalTime.parse(delDataIncreasingTime.get(delDataIncreasingTime.size()-1).rdDeliveryTime);
Duration duration = new Duration.between(firstStop, lastStop); ***<-----ISSUE***
between
是Duration
类的静态工厂方法,返回一个Duration
对象。
因此,您不需要使用new
关键字来创建Duration
对象。
Duration duration = Duration.between(firstStop, lastStop);