本文介绍了如何在Java中找到两个时间戳的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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,但仍然给我一个错误类型为String的getTime方法未定义.

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.

我的问题是这种减法不起作用,它给出了一个错误,无法在原始类型long上调用getTime()方法.对于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中找到两个时间戳的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 10:48