Search code examples
javatimedurationlocaltimetemporal

Java Duration Class, cannot resolve to a type


I am trying to find the difference in time between two LocalTime objects in HH:MM:SS format and my duration object is giving me the error Duration.between cannot be resolved to a type. The between description on Java docs explicitly says LocalTime objects are valid- do I have to translate my objects into LocalDateTime objects with zones for Duration to work?

Only other theory on this issue would be my placement of this class in my Make file, but I would think my import would take care of the issue during compilation. Code below:

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***

Solution

  • between is a static factory method of Duration class, returning a Duration object.

    So you don't need to create Duration object using the new keyword.

    Duration duration = Duration.between(firstStop, lastStop);