我有一个包含几个时间戳的ArrayList,目的是找到ArrayList的第一个和最后一个元素的区别.
String a = ArrayList.get(0); String b = ArrayList.get(ArrayList.size()-1); long diff = b.getTime() - a.getTime();
我还将类型转换为int但仍然给我一个错误方法getTime未定义类型String.
附加信息 :
我有一个A类,其中包括
String timeStamp = new SimpleDateFormat("ss S").format(new Date());
并且有一个B类,它有一个方法private void dialogDuration(String timeStamp)
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类中创建,以便它调用dialogDuration(timeStamp)方法并将时间戳的值传递给B类.
我的问题是这个减法不起作用,它给出了一个错误,无法在原始类型上调用getTime()方法.它也为int和String类型提供了同样的错误?
非常感谢提前!
解决方法
也许是这样的:
SimpleDateFormat dateFormat = new SimpleDateFormat("ss S"); Date firstParsedDate = dateFormat.parse(a); Date secondParsedDate = dateFormat.parse(b); long diff = secondParsedDate.getTime() - firstParsedDate.getTime();