I'm having issues finding an elegant way to subtract one LocalTime from another, but not by some amount of seconds, or minutes, but just this:
LocalTime difference = now - before;
Obviously this won't work, so in reality, what I'm after, is something that will take in two LocalTime's, and give me one back, so I can do the following:
00:01:42:519
00:02:13:217
00:00:30:698
I have tried the following:
long seconds = before.until(now, ChronoUnit.SECONDS);
But obviously that's only going to get me the seconds, ok, so I changed it to:
long millis = before.until(now, ChronoUnit.MILLIS);
long seconds = before.until(now, ChronoUnit.SECONDS);
long minutes = before.until(now, ChronoUnit.MINUTES);
long hours = before.until(now, ChronoUnit.HOURS);
But then I realised its just counting the entire time change in the specified unit, instead of just that units "contribution" to the change, if you understand what I mean.
PS: I've scanned SO for a few questions and they seem to be the same thing, only giving the entire difference back in a singular unit. If this is a duplicate, please point me in the right direction!
Any ideas?
This should do it:
LocalTime diff= t2.minusNanos(t1.toNanoOfDay());
https://ideone.com/3JB3U5
Reading the JavaDoc of LocalTime#until(), I found this solution:
long differenceMillis = MILLIS.between(before, now);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With