javaandroiddatetimedatetime-comparison

How to check for this Date Time Pattern if some minutes are over for Java Android


I receive this Date Time pattern from Server as a string.

Sa. 07.01.2023 16:39:15

Now i want to check if 1 minute is over. Like if the gap between the current time and the time (received as a string) from server is longer than a minute.

The time zone is in Europe. Like Austria.


Solution

    1. Parse the given date-time string into a LocalDateTime.
    2. Convert the obtained LocalDateTime into a ZonedDateTime by applying a ZoneId.
    3. Get the current ZonedDateTime in the applied ZoneId.
    4. Finally, find the minutes between the current ZonedDateTime and the ZonedDateTime obtained from the date-time string.

    Demo:

    import java.time.LocalDateTime;
    import java.time.ZoneId;
    import java.time.ZonedDateTime;
    import java.time.format.DateTimeFormatter;
    import java.time.temporal.ChronoUnit;
    import java.util.Locale;
    
    class Main {
        public static void main(String[] args) {
            String strDateTime = "Sa. 07.01.2023 16:39:15";
    
            DateTimeFormatter parser = DateTimeFormatter.ofPattern("EEE dd.MM.uuuu HH:mm:ss", Locale.GERMAN);
    
            // Note: change the ZoneId as per your requirement
    
            ZonedDateTime zdt = LocalDateTime.parse(strDateTime, parser)
                                             .atZone(ZoneId.of("Europe/Vienna"));
            System.out.println("Date-time received from the server: " + zdt);
    
            ZonedDateTime now = ZonedDateTime.now(zdt.getZone());
            System.out.println("Current date-time: " + now);
    
            System.out.println(ChronoUnit.MINUTES.between(zdt, now) > 1);
        }
    }
    

    Output from a sample run:

    Date-time received from the server: 2023-01-07T16:39:15+01:00[Europe/Vienna]
    Current date-time: 2023-01-07T17:33:04.140599+01:00[Europe/Vienna]
    true
    

    ONLINE DEMO

    Learn more about the modern Date-Time API from Trail: Date Time.


    Incorporating the following valuable alternative solution suggested by Basil Bourque:

    Might be more clear if you switch to Instant objects at the end. Extract an Instant from your first ZonedDateTime, and change ZonedDateTime now to Instant now.

    class Main {
        public static void main(String[] args) {
            String strDateTime = "Sa. 07.01.2023 16:39:15";
    
            DateTimeFormatter parser = DateTimeFormatter.ofPattern("EEE dd.MM.uuuu HH:mm:ss", Locale.GERMAN);
    
            // Note: change the ZoneId as per your requirement
    
            Instant instantParsed = LocalDateTime.parse(strDateTime, parser)
                                                 .atZone(ZoneId.of("Europe/Vienna"))
                                                 .toInstant();
            System.out.println("Instant received from the server: " + instantParsed);
    
            Instant instantNow = Instant.now();
            System.out.println("Current instant: " + instantNow);
    
            System.out.println(ChronoUnit.MINUTES.between(instantParsed, instantNow) > 1);
        }
    }
    

    ONLINE DEMO