问题描述
我有一个 ArrayList
包括几个时间戳,目的是找到 ArrayList
的第一个和最后一个元素的差异.
I have an ArrayList
including several number of time-stamps and the aim is finding the difference of the first and the last elements of the ArrayList
.
String a = ArrayList.get(0);
String b = ArrayList.get(ArrayList.size()-1);
long diff = b.getTime() - a.getTime();
我也将类型转换为 int 但它仍然给我一个错误 getTime 方法未定义为 String 类型
.
I also converted the types to int but still it gives me an error The method getTime is undefined for the type String
.
附加信息:
我有一个 A 类,其中包括
I have a class A which includes
String timeStamp = new SimpleDateFormat("ss S").format(new Date());
并且有一个 B 类,它有一个方法 private void dialogDuration(String timeStamp)
and there is a class B which has a method private void dialogDuration(String timeStamp)
和 dialogueDuration
方法包括:
String a = timeSt.get(0); // timeSt is an ArrayList which includes all the timeStamps
String b = timeSt.get(timeSt.size()-1); // This method aims finding the difference of the first and the last elements(timestamps) of the ArrayList (in seconds)
long i = Long.parseLong(a);
long j = Long.parseLong(b);
long diff = j.getTime()- i.getTime();
System.out.println("a: " +i);
System.out.println("b: " +j);
并且一个条件是语句(String timeStamp = new SimpleDateFormat("ss S").format(new Date());
) 在类 A 中不会被改变.并且一个对象类 B 在类 A 中创建,以便它调用 dialogueDuration(timeStamp)
方法并将时间戳的值传递给类 B.
And one condition is that the statement(String timeStamp = new SimpleDateFormat("ss S").format(new Date());
) wont be changed in class A. And an object of class B is created in class A so that it invokes the dialogueDuration(timeStamp)
method and passes the values of time-stamps to class B.
我的问题是这个减法不起作用,它给出了一个错误cannot invoke getTime() method on the original type long
.对于 int 和 String 类型,它也给出了同样的错误?
My problem is this subtraction does not work, it gives an error cannot invoke getTime() method on the primitive type long
. It gives the same kind of error also for int and String types?
非常感谢!
推荐答案
可能是这样的:
SimpleDateFormat dateFormat = new SimpleDateFormat("ss S");
Date firstParsedDate = dateFormat.parse(a);
Date secondParsedDate = dateFormat.parse(b);
long diff = secondParsedDate.getTime() - firstParsedDate.getTime();
这篇关于如何在java中找到两个时间戳的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!