11

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); 

そして、statement( String timeStamp = new SimpleDateFormat("ss S").format(new Date());) がクラス A で変更されないことが 1 つの条件です。また、クラス B のオブジェクトがクラス A で作成され、dialogueDuration(timeStamp)メソッドが呼び出され、タイムスタンプの値がクラス B に渡されます。

私の問題は、この減算が機能せず、エラーが発生することcannot invoke getTime() method on the primitive type longです。int 型と String 型でも同じ種類のエラーが発生しますか?

よろしくお願いします!

4

4 に答える 4

13

多分このように:

SimpleDateFormat dateFormat = new SimpleDateFormat("ss S");
Date firstParsedDate = dateFormat.parse(a);
Date secondParsedDate = dateFormat.parse(b);
long diff = secondParsedDate.getTime() - firstParsedDate.getTime();
于 2013-02-11T11:17:30.343 に答える
-1

ArrayList xにする必要がありますArrayList<TimeStamp> x。その後、メソッドは( type ではなく) get(int)type のオブジェクトを返します。では、を呼び出すことができます。TimeStampStringTimeStampgetTime()

ところで、本当に必要java.sql.TimeStampですか?DateシンプルなorのCalendar方が簡単で適切かもしれません。

于 2013-02-11T10:20:41.857 に答える