-2

I want to get the following output in android:

val oldTime = timeStoredInSharedPreference() // format : DD:MM:YYYY HH:MM:SS
val newTime = currentTime() // format : DD:MM:YYYY HH:MM:SS

val output = newTime - OldTime // Format should be - in minutes or seconds

also, how do I get the current time in the format DD:MM:YYYY HH:MM:SS?

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
coder
  • 11
  • 5
  • 1
    What are `currentTime()` and `timeStoredInSharedPreference()`? Long, Date, ?? – Skizo-ozᴉʞS ツ Jul 24 '20 at 10:37
  • 2
    Does this answer your question? [Difference between two datetime formats in android?](https://stackoverflow.com/questions/13216263/difference-between-two-datetime-formats-in-android) – Marvin Jul 24 '20 at 13:49

3 Answers3

3

Since Java 8 you can calculate it with LocalDateTime and Duration

LocalDateTime now = LocalDateTime.now();
LocalDateTime sixMinutesBehind = now.minusMinutes(6);

Duration duration = Duration.between(now, sixMinutesBehind);
long diff = Math.abs(duration.toMinutes());
Tobi
  • 41
  • 1
  • 4
1

With Java-9 onwards, you can use Duration#toMinutes and Duration#toSeconds. Note that Duration#toMinutes has been there since Java-8 but Duration#toSeconds came with Java-9, and therefore you can use Duration#toMillis / 1000 instead of Duration#toSeconds if you are using Java-8.

Demo:

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        String strOldDateTime = "24:7:2020 10:10:10";

        // Define the format
        DateTimeFormatter format = DateTimeFormatter.ofPattern("d:M:yyyy HH:mm:ss");

        // Parse date-time as using the defined format
        LocalDateTime oldDateTime = LocalDateTime.parse(strOldDateTime, format);

        // Now
        LocalDateTime now = LocalDateTime.now();

        Duration duration = Duration.between(oldDateTime, now);

        // Display the difference in minutes and seconds

        // ###########Java-8#############
        System.out.println(duration.toMinutes() + " minutes " + (duration.toMillis() / 1000) % 60);

        // ###########Java-9 onwards#############
        System.out.println(duration.toMinutes() + " minutes " + duration.toSeconds() % 60);
    }
}

Output:

106 minutes 17
106 minutes 17
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • On Java 8 you can use `Duration#getSeocnds` to get the same result as the one from `Duration#toSeconds` from Java 9. It was a bit confusing. In Java 9 they seem to have realized that they needed method named consistently with the other `toXxx` methods. – Ole V.V. Jul 24 '20 at 19:32
  • Thanks, Ole V. V. for the valuable input as usual. – Arvind Kumar Avinash Jul 24 '20 at 21:27
1
@Test
fun test() {
    val dateTimeFormatter = DateTimeFormatter.ofPattern("dd:MM:yyyy HH:mm:ss")
    val oldTime = LocalDateTime.parse("24:07:2020 11:22:33", dateTimeFormatter)
    val newTime = LocalDateTime.parse("24:07:2020 11:32:33", dateTimeFormatter)

    Duration.between(oldTime, newTime).seconds shouldBe 10 * 60
}
Frank Neblung
  • 3,047
  • 17
  • 34