Java Duration 类,无法解析为类型

发布于 2025-01-14 09:24:19 字数 1214 浏览 4 评论 0原文

我试图找出 HH:MM:SS 格式的两个 LocalTime 对象之间的时间差,并且我的持续时间对象给了我错误Duration. Between 无法解析为类型。 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***

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请恋爱 2025-01-21 09:24:19

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

因此,您不需要使用 new 关键字创建 Duration 对象。

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文