0

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

1 Answer 1

5

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);
Sign up to request clarification or add additional context in comments.

7 Comments

That's it, I looked too deep first. What does your process for diagnosing a bug look like? I need a better approach
Use a proper IDE (Intellij, Eclipse, ...), it will tell you such minor mistakes immediately.
I'm using VS code, so I definitely don't have the same level of support for Java. I like being able to work on several different languages in the same IDE, would you recommend I get tailored IDE's? I primarily work with the MEAN stack
Since Java allows you to call static methods via an instance, this is not the entire answer. The error message comes from the OP trying to create an instance of some nested class of Duration named between (note what follows the new keyword), which obviously doesn't exist.
@CoolHands I didn't run your code, but I just got what the problem is in Duration.between statement
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.