javaandroid-studiodatetimerfc3339

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


I am importing some sensor-data in my app over a server with JSON objects. They contain the value and time of the last measurement.

The time is in RFC3339 format send as a string, e.g. 2020-06-19T15:32:25.528Z.

Now, instead of displaying the last measurement´s time, I want to show the time difference between now and the measurement.

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. The only thing I saw is this article, but couldn´t manage to import the datetime-class in my app.

Can anybody help?


Solution

  • 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.

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

    Output:

    3 hours 3 minutes 43 seconds
    

    [Update]

    Note that Duration#toXXXXXPart() e.g. Duration#toHoursPart() was introduced with Java-9. If you are using Java-8, you can use the calculation shown below:

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

    [Another Update] Courtesy 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());
        }
    }