我如何获得在android的两个日期之间的差异?尝试每一件事情和后期每一件、如何获得、后期、差异

2023-09-05 03:23:59 作者:作

我看到所有的职位这里我还是不明白怎么弄两款Android日期之间的差异。

I saw all the post in here and still I can't figure how do get difference between two android dates.

这是我做的:

long diff = date1.getTime() - date2.getTime();
Date diffDate = new Date(diff);

和我得到:日期为1970年1月1日,时间永远是更大的两个小时......我从以色列很两小时timeOffset

and I get: the date is Jan. 1, 1970 and the time is always bigger in two hours...I'm from Israel so the two hours is timeOffset.

我怎样才能得到正常的区别???

How can I get normal difference???

推荐答案

您正在接近正确的答案,你得到这两个日期之间的毫秒的差异,但是当你试图建构一个日期了这种差异,这是假设您想要创建与差值作为其划时代一次新日期对象。如果你正在寻找小时的时间,那么你就只需要做一些基本的算术差异,以获得不同的时间部分:

You're close to the right answer, you are getting the difference in milliseconds between those two dates, but when you attempt to construct a date out of that difference, it is assuming you want to create a new Date object with that difference value as its epoch time. If you're looking for a time in hours, then you would simply need to do some basic arithmetic on that diff to get the different time parts:

long diff = date1.getTime() - date2.getTime();
long seconds = diff / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
long days = hours / 24;

这一切数学就干脆做整数运算,因此它会截断任何小数点

All of this math will simply do integer arithmetic, so it will truncate any decimal points