如何在 Android Studio 中计算现在和 RFC3339 时间戳之间的差异?

How to calculate difference between now and a RFC3339 timestamp in Android Studio?

我正在通过具有 JSON 个对象的服务器在我的应用程序中导入一些传感器数据。它们包含上次测量的值和时间。

时间采用 RFC3339 格式作为字符串发送,例如2020-06-19T15:32:25.528Z.

现在,我不想显示上次测量的时间,而是想显示现在和[=]之间的时差 23=]测量.

通过搜索,我没有找到将 RFC3339 时间戳字符串解析为可编辑格式的好的解决方案,也没有找到计算与现在的差异的方法。 我唯一看到的是 this 文章,但无法在我的应用程序中导入 datetime-class。

有人可以帮忙吗?

Searching I found neither a good solution to parse a RFC3339 timestamp string to an editable format, nor a way to calculate the difference to now.

使用java.time.Duration

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

public class Main {
    public static void main(String[] args) {
        // Given date-time
        LocalDateTime ldt = ZonedDateTime
                .parse("2020-06-19T15:32:25.528Z",
                        DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSz").withZone(ZoneId.systemDefault()))
                .toLocalDateTime();

        // Get the duration between the given date-time and now
        Duration duration = Duration.between(ldt, LocalDateTime.now(ZoneId.systemDefault()));

        // Get hours, minutes, seconds from the Duration object
        System.out.printf("%d hours %d minutes %d seconds%n", duration.toHoursPart(), duration.toMinutesPart(),
                duration.toSecondsPart());
    }
}

输出:

3 hours 3 minutes 43 seconds

[更新]

请注意 Duration#toXXXXXPart() 例如Duration#toHoursPart() 与 Java-9 一起引入。如果您使用的是Java-8,则可以使用如下所示的计算:

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

public class Main {
    public static void main(String[] args) {
        // Given date-time
        LocalDateTime ldt = ZonedDateTime
                .parse("2020-06-19T15:32:25.528Z",
                        DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSz").withZone(ZoneId.systemDefault()))
                .toLocalDateTime();

        // Get the duration between the given date-time and now
        Duration duration = Duration.between(ldt, LocalDateTime.now(ZoneId.systemDefault()));

        // Get hours, minutes, seconds from the Duration object
        System.out.printf("%d hours %d minutes %d seconds%n", duration.toHoursPart(), duration.toMinutesPart(),
                duration.toSecondsPart());

        // For Java-8
        long hours = duration.toHours();
        int minutes = (int) ((duration.getSeconds() % (60 * 60)) / 60);
        int seconds = (int) (duration.getSeconds() % 60);
        System.out.printf("%d hours %d minutes %d seconds%n", hours, minutes, seconds);
    }
}

[另一个更新] 礼貌

import java.time.Duration;
import java.time.Instant;

public class Main {
    public static void main(String[] args) {
        // Given date-time
        Instant instant = Instant.parse("2020-06-19T15:32:25.528Z");

        // Get the duration between the given date-time and now
        Duration duration = Duration.between(instant, Instant.now());

        // Get hours, minutes, seconds from the Duration object
        System.out.printf("%d hours %d minutes %d seconds%n", duration.toHoursPart(), duration.toMinutesPart(),
                duration.toSecondsPart());
    }
}