-1

I have a date string looking like this:

String time1 = "2018-08-28T10:23:25.617Z";

I want to get the difference between this date to the current date in utc time. What is the easiest way to do it? thanks.

xingbin
  • 27,410
  • 9
  • 53
  • 103
Gal Itzhak
  • 449
  • 1
  • 7
  • 14
  • [Java string to date conversion](//stackoverflow.com/q/4216745) – 001 Aug 28 '18 at 15:02
  • 1
    Please use your search engine. I’m sure you can find questions and answer that match your exact requirements better than what we have found and linked to. It’s not clear whether you want the difference in days or in milliseconds or something else. – Ole V.V. Aug 28 '18 at 17:14
  • Search before posting. This topic has been addressed many many times already. You can assume any basic date-time question has been asked and answered. – Basil Bourque Aug 29 '18 at 02:32
  • 1
    Also duplicate of [this](https://stackoverflow.com/q/2201925/642706) and [this](https://stackoverflow.com/q/1770010/642706) and [this](https://stackoverflow.com/q/567659/642706) and [this](https://stackoverflow.com/q/8405087/642706). – Basil Bourque Aug 29 '18 at 02:38
  • @BasilBourque Most of the answers to the "duplicated questions" are using 'SimpleDateFormat' which is legacy, and also didn't work for me. And also, I think one person that says it's duplicate is enough, no need to 'duplicate' previous comments. – Gal Itzhak Aug 30 '18 at 06:47

1 Answers1

4

You can use java.time.Instant and java.time.Duration:

Instant instant = Instant.parse("2018-08-28T10:23:25.617Z"); // start time in UTC
Instant now = Instant.now();  // end time in UTC

Duration duration = Duration.between(instant, now); // the Duration

System.out.println(duration.toSeconds()); // duration in seconds
xingbin
  • 27,410
  • 9
  • 53
  • 103