Java subtract LocalTime

Bob picture Bob · Feb 5, 2015 · Viewed 30.2k times · Source

I have two LocalTime objects:

LocalTime l1 = LocalTime.parse("02:53:40");
LocalTime l2 = LocalTime.parse("02:54:27");

How can I found different in minutes between them?

Answer

Yosef Weiner picture Yosef Weiner · Feb 5, 2015

Use until or between, as described by the 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