0

I have two UNIX time stamp and i am using KOTLIN

1) old time - 1534854646 2) current time - 1534857527

Now i want the difference in hours and minutes.

 val result = DateUtils.getRelativeTimeSpanString(1534854646, 1534857527, 0)

But it gives me 2 seconds but actual time difference is around 0 hour and 48 minutes.

I have also tried :

long mills = 1534857527 - 1534854646;
int hours = millis/(1000 * 60 * 60);
int mins = (mills/(1000*60)) % 60;

String diff = hours + ":" + mins; 

But still it gives 0 hours and 0 minute.

  • follow this link : https://stackoverflow.com/questions/44341891/java-android-calculate-difference-between-timestamps/44342317#44342317 – Aniruddh Parihar Aug 21 '18 at 13:43
  • I think your timestamps are in seconds, not milliseconds – David Zimmerman Aug 21 '18 at 14:50
  • Your first calculation ends up with a result under 0. `2881 / ((1000 * 60) * 60) = 0.00080027778`. Which means this is a duplicate. – Zoe Aug 21 '18 at 18:49
  • 2
    Possible duplicate of [Why is the result of 1/3 == 0?](https://stackoverflow.com/questions/4685450/why-is-the-result-of-1-3-0) – Zoe Aug 21 '18 at 18:49

3 Answers3

6

Here is my solution, the code is written in Kotlin.

TimeInHours.kt

class TimeInHours(val hours: Int, val minutes: Int, val seconds: Int) {
        override fun toString(): String {
            return String.format("%dh : %02dm : %02ds", hours, minutes, seconds)
        }
}

Write a function which converts time duration in seconds to TimeInHours.

fun convertFromDuration(timeInSeconds: Long): TimeInHours {
        var time = timeInSeconds
        val hours = time / 3600
        time %= 3600
        val minutes = time / 60
        time %= 60
        val seconds = time
        return TimeInHours(hours.toInt(), minutes.toInt(), seconds.toInt())
}

Test.kt

val oldTime: Long = 1534854646
val currentTime: Long = 1534857527
val result = convertFromDuration(currentTime - oldTime)
Log.i("TAG", result.toString())

Output:

I/TAG: 0h : 48m : 01s
Son Truong
  • 13,661
  • 5
  • 32
  • 58
  • this answer was perfect...ty for sharing!. I'll add a new answer widening your answer friend...take a look if u like. – Ramiro G.M. May 04 '23 at 19:43
1

Do something like this, I have not tested this but it should work

    long mills = 1534857527 - 1534854646;
    String period = String.format("%02d:%02d", 
        TimeUnit.MILLISECONDS.toHours(mills),
        TimeUnit.MILLISECONDS.toMinutes(mills) % TimeUnit.HOURS.toMinutes(1));

    System.out.println("Duration hh:mm -  " + period);
Qasim
  • 5,181
  • 4
  • 30
  • 51
0

Based on Son Truong's correct answer above, I added the fun inside the class to have everything inside the same class, and added support for days:

class TimeInHours(val days: Int, val hours: Int, val minutes: Int, val seconds: Int) {
companion object {
    fun convertFromDuration(timeInSeconds: Long): TimeInHours {
        var time = timeInSeconds
        val days = time / 86400
        time %= 86400
        val hours = time / 3600
        time %= 3600
        val minutes = time / 60
        time %= 60
        val seconds = time
        return TimeInHours(days.toInt(), hours.toInt(), minutes.toInt(), seconds.toInt())
    }
}

override fun toString(): String {
    return String.format("%dd : %dh : %02dm : %02ds", days, hours, minutes, seconds)
}

}

Usage:

val currentTime: Long = System.currentTimeMillis() / 1000
val oldTime: Long = 1682613120
val result = TimeInHours.convertFromDuration(currentTime - oldTime)
print("Result: "+ result.toString())

Output:

Result: 7d : 3h : 16m : 28s
Ramiro G.M.
  • 357
  • 4
  • 7