【问题标题】:How to calculate difference between now and a RFC3339 timestamp in Android Studio?如何计算现在与 Android Studio 中的 RFC3339 时间戳之间的差异?
【发布时间】:2020-10-10 00:04:30
【问题描述】:

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

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

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

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

有人可以帮忙吗?

【问题讨论】:

    标签: java android-studio datetime rfc3339


    【解决方案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() 例如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);
        }
    }
    

    [另一个更新]礼貌Ole V.V.

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

    【讨论】:

    • 非常感谢您的想法。只有一个问题:尝试实施您的建议,Android Studio 告诉我有 no to...Part()-方法,甚至没有 toSeconds()-方法。尽管我使用了与您相同的导入,并且 Oracle 知道这种方法。 知道为什么吗?
    • @Anonymix - 我已经发布了更新。我希望,它有帮助。如有任何疑问/问题,请随时发表评论。
    • (1) LocalDateTime 是使用错误的类,因为它忽略了源字符串中的偏移量,这对于了解正确的结果至关重要。我推荐Instant。 (2) 您不需要指定格式化程序。 Instant.parse() 和单参数 OffsetDateTime.parse() 中的每一个都将在没有任何显式格式化程序的情况下解析 RFC-3339 字符串。
    • 谢谢,@OleV.V。一如既往的宝贵建议。我已将其作为更新合并到答案中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    • 2020-05-13
    • 1970-01-01
    • 2014-05-04
    相关资源
    最近更新 更多