Java Duration 类,无法解析为类型
我试图找出 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Between
是Duration
类的静态工厂方法,返回一个Duration
对象。因此,您不需要使用
new
关键字创建Duration
对象。between
is a static factory method ofDuration
class, returning aDuration
object.So you don't need to create
Duration
object using thenew
keyword.