3

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?

  • 1
    I am sorry, you have misunderstood completely. A `LocalTime` is a time of day. The difference between two times is not a `LocalTime`, it is called a `Duration`. – Ole V.V. Dec 03 '17 at 22:41
  • @OleV.V. Yes, I know that, but when I call durations and print them, theres a weird "P" thing at the beginning and the formatting is out of wack, I only want to know the "Time of day" to make parsing life easier, where I can format it to what I want with `DateTimeFormatter` afterwards. –  Dec 03 '17 at 22:42
  • 1
    I recommend you solve that problem instead of misusing `LocalTime` and thereby inventing a different problem. The question of formatting a `Duration` has been asked and answered more than once, see for example [How to format a duration in java? (e.g format H:MM:SS)](https://stackoverflow.com/questions/266825/how-to-format-a-duration-in-java-e-g-format-hmmss). – Ole V.V. Dec 03 '17 at 22:48
  • @OleV.V. Thankyou for the link. My only question is, what problem could arise from using `LocalTime` in this fashion? After the step of getting it, all I do is print it to command line. Of-course I would like to do it correctly so I'll have a look at Duration. One more thing, would it be inherently faster, in terms of runtime speed to use Duration instead? –  Dec 03 '17 at 22:51
  • 1
    I would first and foremost fear that you will confuse anyone reading and trying to maintain your code in the future. For the speed I don’t know, if this matters to you, you will have to make your own measurements. – Ole V.V. Dec 03 '17 at 23:22
  • @OleV.V. Yes, alright. Well thank you for the implementation! –  Dec 03 '17 at 23:25
  • 1
    The idea to misuse `LocalTime` as duration will also kill you if the involved input times originate from different days. This is even true for the simple case of difference between 23PM and 1AM next day. Your code would produce 22 hours, but it is really just two hours. Using a dedicated duration formatter makes your life much easier. Agreed, standard Java has no real duration formatter but either some workarounds as described in Oles link or external libs like JodaTime or my lib Time4J would help. – Meno Hochschild Dec 04 '17 at 12:02
  • 1
    @MenoHochschild Oh hang on, I read that and was about to dismiss the part about days, because I'm only implementing this for an application that would run at most for a few minutes, but if it's going to do that for a 2 hour gap then jeez, I'm definitely doing this right now. –  Dec 05 '17 at 06:44

3 Answers3

3

This should do it:

LocalTime diff= t2.minusNanos(t1.toNanoOfDay());

https://ideone.com/3JB3U5

vandale
  • 3,600
  • 3
  • 22
  • 39
  • Oh, my, god. It's literally that simple... Thankyou very much! –  Dec 03 '17 at 08:10
  • One more thing, it's got a millisecond resolution of 5 digits, any way I can decrease it to 3? –  Dec 03 '17 at 08:11
  • Sorry for downvote, but never misuse `DateTimeFormatter` as kind of duration formatter. Some edge cases will fail, here for example if the times t1 and t2 are from different days. – Meno Hochschild Dec 04 '17 at 12:05
1

Reading the JavaDoc of LocalTime#until(), I found this solution:

long differenceMillis = MILLIS.between(before, now);
Ridcully
  • 23,362
  • 7
  • 71
  • 86
  • Yes, but that is the issue I addressed in my question. I don't want the millisecond difference, I want a `LocalTime`. –  Dec 03 '17 at 08:14
  • This only gives me the entire time difference in milliseconds. What I'm after, is the difference in the respective hours, minutes, seconds and milliseconds. –  Dec 03 '17 at 08:14
0
    Instant start = Instant.now();
        //your lengthy code here
    Instant end = Instant.now();
    long nanoDiff = end.getNano() - start.getNano();

Why did I post this answer? B/c you can play with Instant class and get so many kinds of different format as an answer to your question...

zlakad
  • 1,314
  • 1
  • 9
  • 16