2

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.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
DealZg
  • 136
  • 1
  • 3
  • 14
  • 2
    Do we know in which time zone that string was created? Depending on time zone it may denote a time within a span of some 26 hours. – Ole V.V. Jan 07 '23 at 16:10
  • The time zone is in Europe. Like Austria – DealZg Jan 07 '23 at 16:11
  • How would you do that if you tell you a time and you're supposed to check if a certain amount of minutes as passed? I guess you would "parse" my information as a datetime and then compare the time difference with "now()"? Java/Android can do that as well and those steps are researchable. Have you done some research? Tried anything? – Tom Jan 07 '23 at 16:11
  • 2
    `LocalDateTime.parse("Sa. 07.01.2023 16:39:15", DateTimeFormatter.ofPattern("E dd.MM.uuuu H:mm:ss", Locale.GERMAN)) .atZone(ZoneId.systemDefault()) .plusMinutes(1) .isBefore(ZonedDateTime.now())`. – Ole V.V. Jan 07 '23 at 16:14
  • 1
    Or instead of `ZoneId.systemDefault()` we need to give `ZoneId.of("Europe/Vienna")` if you know that this is the server’s time zone. – Ole V.V. Jan 07 '23 at 16:18
  • it allways shows false – DealZg Jan 07 '23 at 16:39
  • Weird. It produces `true` on my Java 18. Happy that you accepted the answer, though. I take it to mean you got your question answered. – Ole V.V. Jan 08 '23 at 11:59

1 Answers1

6
  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

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • the time zone can be different. people from all over the world have access. its only for their current time zone. it can be everywhere – DealZg Jan 07 '23 at 16:41
  • 1
    @DealZg - If your use case is to find the difference between the date-times ignoring the timezone, you can find the difference between the parsed string (i.e. `LocalDateTime.parse(strDateTime, parser)`) and `LocalDateTime.now()`. Please keep in mind that the current date-time in America is different from the current date-time in Austria. So, most use cases are to compare the date-times at the same timezone. – Arvind Kumar Avinash Jan 07 '23 at 16:51
  • 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`. – Basil Bourque Jan 07 '23 at 17:25