计算两次之间的差异时得到不正确的结果

时间:2021-11-11 21:31:44

I am working on an android app to keep track of hours between signing in and out. An employee can sign in one day and sign out the next day or within the same day.

我正在开发一个Android应用程序来跟踪登录和退出之间的小时数。员工可以在一天内登记,并在第二天或同一天内退出。

Within the app, I have a function that should calculate the difference between two times.

在应用程序中,我有一个函数,应该计算两次之间的差异。

But I am not getting the correct results...

但我得不到正确的结果......

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("h:mma");

    Date start;
    Date end;
    double difference;

    //Test 1
    start = simpleDateFormat.parse("7:00AM");
    end = simpleDateFormat.parse("3:30PM");
    difference = end.getTime() - start.getTime();
    Log.d("difference", String.valueOf(difference)); // 3.06E7
    Log.d("Time difference", String.valueOf(((difference/1000)/60)/60));
    //Prints '8.5' correct.

    // Text 2
    start = simpleDateFormat.parse("11:00PM");
    end = simpleDateFormat.parse("7:30AM");
    difference = end.getTime() - start.getTime();
    Log.d("difference2", String.valueOf(difference)); // -5.58E7
    Log.d("Time difference2", String.valueOf(((difference/1000)/60)/60));
    //Prints '-15.5' but should print '8.5'

As you can see... the first test works correctly, yet the second test fails. How can I resolve this issue?

如您所见......第一次测试正常,但第二次测试失败。我该如何解决这个问题?

2 个解决方案

#1


2  

Your current issue is that your are calculating the difference between two hours and these hours are technically on the same day.

您当前的问题是您正在计算两小时之间的差异,而这些小时在技术上是在同一天。

You are currently calculating the time difference between (A) Day 1: 23:00 and (b) Day 1: 7:30. 7:30 is 15.5 hours before 23:00.

您目前正在计算(A)第1天:23:00和(b)第1天:7:30之间的时差。 7:30是23:00之前的15.5小时。

You will need to add your starting and ending dates.

您需要添加开始和结束日期。

Exemple:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

Date day1 = simpleDateFormat.parse("12/10/2017 23:00:00");
Date day2 = simpleDateFormat.parse("13/10/2017 07:30:00");

DateTime dt1 = new DateTime(day1);
DateTime dt2 = new DateTime(day2);

System.out.print(Hours.hoursBetween(dt1, dt2).getHours() % 24 + " hours, ");
System.out.print(Minutes.minutesBetween(dt1, dt2).getMinutes() % 60 + " minutes.");
// Should print "8 hours, 30 minutes.'

#2


1  

start is after end (on same day), hence a negative result; -15.5 + 24 == 8.5. A modulo 24 is in order:

开始是在结束之后(同一天),因此是负面结果; -15.5 + 24 == 8.5。模数24按顺序排列:

difference = end.getTime() - start.getTime();
difference = difference/1000/60/60;
if (difference < 0) {
    difference += 24;
}
Log.d("Time difference", String.valueOf(difference));

#1


2  

Your current issue is that your are calculating the difference between two hours and these hours are technically on the same day.

您当前的问题是您正在计算两小时之间的差异,而这些小时在技术上是在同一天。

You are currently calculating the time difference between (A) Day 1: 23:00 and (b) Day 1: 7:30. 7:30 is 15.5 hours before 23:00.

您目前正在计算(A)第1天:23:00和(b)第1天:7:30之间的时差。 7:30是23:00之前的15.5小时。

You will need to add your starting and ending dates.

您需要添加开始和结束日期。

Exemple:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

Date day1 = simpleDateFormat.parse("12/10/2017 23:00:00");
Date day2 = simpleDateFormat.parse("13/10/2017 07:30:00");

DateTime dt1 = new DateTime(day1);
DateTime dt2 = new DateTime(day2);

System.out.print(Hours.hoursBetween(dt1, dt2).getHours() % 24 + " hours, ");
System.out.print(Minutes.minutesBetween(dt1, dt2).getMinutes() % 60 + " minutes.");
// Should print "8 hours, 30 minutes.'

#2


1  

start is after end (on same day), hence a negative result; -15.5 + 24 == 8.5. A modulo 24 is in order:

开始是在结束之后(同一天),因此是负面结果; -15.5 + 24 == 8.5。模数24按顺序排列:

difference = end.getTime() - start.getTime();
difference = difference/1000/60/60;
if (difference < 0) {
    difference += 24;
}
Log.d("Time difference", String.valueOf(difference));