localdatetime - Java resta LocalTime
set local time java (4)
Desde Java 8 puedes usar la clase Duration
. Creo que eso da la solución más elegante:
long elapsedMinutes = Duration.between(l1, l2).toMinutes();
Tengo dos objetos LocalTime
:
LocalTime l1 = LocalTime.parse("02:53:40");
LocalTime l2 = LocalTime.parse("02:54:27");
¿Cómo puedo encontrar diferentes en minutos entre ellos?
Podrías hacer esto:
long dif = Math.abs (l1.getLocalMillis () - l2.getLocalMillis ());
TimeUnit.MINUTES.convert (dif, TimeUnit.MILLISECONDS);
Use until
o between
, como lo describe la api
import java.time.LocalTime;
import static java.time.temporal.ChronoUnit.MINUTES;
public class SO {
public static void main(String[] args) {
LocalTime l1 = LocalTime.parse("02:53:40");
LocalTime l2 = LocalTime.parse("02:54:27");
System.out.println(l1.until(l2, MINUTES));
System.out.println(MINUTES.between(l1, l2));
}
}
0
0
Hago esto con ChronoUnit
long minutesBetween = ChronoUnit.MINUTES.between(l1,l2);
Ejemplo
LocalTime localTime=LocalTime.now();
LocalTime localTimeAfter5Minutes=LocalTime.now().plusMinutes(5);
Long minutesBetween=ChronoUnit.MINUTES.between(localTime,localTimeAfter5Minutes);
System.out.println("Diffrence between time in munutes : "+minutesBetween);
Salida
Diffrence between time in munutes : 5