我有一个包含多个时间戳的 ArrayList
,目的是找到 ArrayList
的第一个和最后一个元素的区别。
String a = ArrayList.get(0);
String b = ArrayList.get(ArrayList.size()-1);
long diff = b.getTime() - a.getTime();
我也将类型转换为 int 但它仍然给我一个错误 The method getTime is undefined for the type String
。
附加信息:
我有一个 A 类,其中包括
String timeStamp = new SimpleDateFormat("ss S").format(new Date());
还有一个 B 类,它有一个方法 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 类。
我的问题是这个减法不起作用,它给出了一个错误cannot invoke getTime() method on the primitive type long
。对于 int 和 String 类型,它也会给出相同类型的错误?
提前致谢!
最佳答案
可能是这样的:
SimpleDateFormat dateFormat = new SimpleDateFormat("ss S");
Date firstParsedDate = dateFormat.parse(a);
Date secondParsedDate = dateFormat.parse(b);
long diff = secondParsedDate.getTime() - firstParsedDate.getTime();
关于java - 如何找到java中两个时间戳的差异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14810084/