有没有内置的方法可以在安卓中检查两个日期时间之间的差异?

huangapple go评论89阅读模式
英文:

is their any inbuilt method to check the difference between two dateTime in android?

问题

我想在 Android 中获得以下输出:

val oldTime = 从共享偏好中获取的时间() // 格式:DD:MM:YYYY HH:MM:SS
val newTime = 当前时间() // 格式:DD:MM:YYYY HH:MM:SS

val output = newTime - oldTime // 格式应为分钟或秒

另外,我如何以 `DD:MM:YYYY HH:MM:SS` 格式获取当前时间?
英文:

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?

答案1

得分: 3

自Java 8以来,您可以使用LocalDateTime和Duration来计算它。

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

Duration duration = Duration.between(now, sixMinutesBehind);
long diff = Math.abs(duration.toMinutes());
英文:

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());

答案2

得分: 1

Java-9 开始,您可以使用 Duration#toMinutesDuration#toSeconds。请注意,Duration#toMinutesJava-8 开始就存在,但 Duration#toSeconds 是从 Java-9 开始引入的,因此如果您在使用 Java-8,可以使用 Duration#toMillis / 1000 代替 Duration#toSeconds

示例:

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";

        // 定义格式
        DateTimeFormatter format = DateTimeFormatter.ofPattern("d:M:yyyy HH:mm:ss");

        // 使用定义的格式解析日期时间
        LocalDateTime oldDateTime = LocalDateTime.parse(strOldDateTime, format);

        // 当前时间
        LocalDateTime now = LocalDateTime.now();

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

        // 显示分钟和秒的差异

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

        // ###########Java-9 及以后版本#############
        System.out.println(duration.toMinutes() + " 分钟 " + duration.toSeconds() % 60);
    }
}

输出:

106 分钟 17
106 分钟 17
英文:

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

答案3

得分: 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
}
英文:
@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
}

huangapple
  • 本文由 发表于 2020年7月24日 18:33:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63071828.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定