1

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

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

现在,我不想显示上次测量的时间,而是想显示现在测量之间的时间差

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

有人可以帮忙吗?

4

1 回答 1

1

搜索我发现既不是将 RFC3339 时间戳字符串解析为可编辑格式的好解决方案,也没有找到计算与现在的差异的方法。

使用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()egDuration#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);
    }
}

[另一个更新]礼貌Ole VV

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());
    }
}
于 2020-06-19T17:32:31.203 回答