Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate days, hours and minutes between two instants

I have an instant formatted like this:

DateTimeFormatter formatter = 
 DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT )
                                 .withLocale( Locale.UK )
                                 .withZone( ZoneId.of("UTC"));

String instant = formatter.format(Instant.now()).toString();

and i want to calculate the days, hours and minutes (if possible) between the instant and Instant.now(), something like:

// for days
String daysBetween = Instant.now() - instant;

can i do that ?

like image 229
Mateus Henrique Avatar asked Apr 21 '19 04:04

Mateus Henrique


People also ask

What is a definite duration of time between two given instants?

Interval between two events is known as Duration. For example : A work start at 10 AM and it ends 12 AM. Duration of work will be 2 hr. Was this answer helpful?

How do you calculate time difference?

To calculate the time difference in minutes, you need to multiply the resulting value by the total number of minutes in a day (which is 1440 or 24*60). Suppose you have a data set as shown below and you want to calculate the total number of minutes elapsed between the start and the end date.

How do you add instant days?

Instant plus() method in Java An immutable copy of a instant where a time unit is added to it can be obtained using the plus() method in the Instant class in Java. This method requires two parameters i.e. time to be added to the instant and the unit in which it is to be added.


1 Answers

To calculate days, hours and/or minutes between two Instant objects:

Instant instant1 = Instant.parse("2019-02-14T18:42:00Z");
Instant instant2 = Instant.parse("2019-04-21T05:25:00Z");

// If you only need one of them
System.out.println(ChronoUnit.DAYS.between(instant1, instant2));    // prints: 65
System.out.println(ChronoUnit.HOURS.between(instant1, instant2));   // prints: 1570
System.out.println(ChronoUnit.MINUTES.between(instant1, instant2)); // prints: 94243

// Or use alternate syntax (it's the same thing)
System.out.println(instant1.until(instant2, ChronoUnit.DAYS));    // prints: 65
System.out.println(instant1.until(instant2, ChronoUnit.HOURS));   // prints: 1570
System.out.println(instant1.until(instant2, ChronoUnit.MINUTES)); // prints: 94243

// Or use Duration
Duration duration = Duration.between(instant1, instant2);
System.out.println(duration.toDays());    // prints: 65
System.out.println(duration.toHours());   // prints: 1570
System.out.println(duration.toMinutes()); // prints: 94243

// In Java 9+, Duration can give them as parts to be used together
System.out.println(duration.toDaysPart());    // prints: 65
System.out.println(duration.toHoursPart());   // prints: 10
System.out.println(duration.toMinutesPart()); // prints: 43

// Or you can calculate the parts yourself, using epoch seconds
long seconds = instant2.getEpochSecond() - instant1.getEpochSecond();
System.out.println(seconds / 86400);     // prints: 65
System.out.println(seconds / 3600 % 24); // prints: 10
System.out.println(seconds / 60 % 60);   // prints: 43

As you can see in the last two, the difference is 65 days, 10 hours, and 43 minutes for the given sample dates.

like image 62
Andreas Avatar answered Oct 21 '22 17:10

Andreas