Java Duration 类,无法解析为类型

Coo*_*nds 0 java time duration localtime temporal

我试图找出 HH:MM:SS 格式的两个 LocalTime 对象之间的时间差,而我的持续时间对象给了我错误 Duration. Between Cannot be returned to a type。Java 文档上的 Between 描述明确表示 LocalTime 对象是有效的 - 我是否必须将我的对象转换为具有 Duration 区域的 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***
Run Code Online (Sandbox Code Playgroud)

Ser*_*nov 5

between是类的静态工厂方法Duration,返回一个Duration对象。

所以你不需要Duration使用关键字创建对象new

Duration duration = Duration.between(firstStop, lastStop);
Run Code Online (Sandbox Code Playgroud)

  • 使用合适的IDE(Intellij,Eclipse,...),它会立即告诉你这样的小错误。 (2认同)