1

Executed piece of code :

    String lapTime = "27:10.190";
    int minutes = Integer.parseInt(lapTime.substring(0, lapTime.indexOf(":")));
    int seconds = Integer.parseInt(lapTime.substring(lapTime.indexOf(":")+1, lapTime.indexOf(".")));
    int milliseconds = Integer.parseInt(lapTime.substring(lapTime.indexOf(".")+1));
    LocalTime localTime = LocalTime.of(0, minutes, seconds, milliseconds);

How to gain number of milliseconds of LocalTime localTime = LocalTime.of(0, minutes, seconds, milliseconds); ?

2 Answers2

3

TL;DR Use Duration, not LocalTime. See end of answer.


Question code is incorrect

Be aware that the 4th argument to LocalTime.of() is nanosecond, not millisecond, which you'd see if you print localTime:

System.out.println(localTime); // prints: 00:27:10.000000190

So you need to change your code to:

LocalTime localTime = LocalTime.of(0, minutes, seconds, milliseconds * 1000000);
System.out.println(localTime); // prints: 00:27:10.190

Using LocalTime

If you wanted the milliseconds value back, call getLong(TemporalField field) with ChronoField.MILLI_OF_SECOND:

localTime.getLong(ChronoField.MILLI_OF_SECOND) // returns 190

To gain total number of milliseconds, i.e. not just the milliseconds value, use ChronoField.MILLI_OF_DAY as argument:

localTime.getLong(ChronoField.MILLI_OF_DAY) // returns 1630190

Using Duration

However, since the input is named lapTime, the LocalTime class is not the right tool for the job. E.g. your code will fail if minutes >= 60.

The right tool is the Duration class, e.g.

Duration duration = Duration.ofMinutes(minutes).plusSeconds(seconds).plusMillis(milliseconds);

Or:

Duration duration = Duration.ofSeconds(seconds, milliseconds * 1000000).plusMinutes(minutes);

You can then get the milliseconds directly by calling toMillis():

duration.toMillis(); // returns 1630190

That works even if the lap time exceeds one hour, e.g.

String lapTime = "127:10.190";
. . .
duration.toMillis(); // returns 7630190

In Java 9+, you can get the millisecond part back easily, by calling toMillisPart():

duration.toMillisPart(); // returns 190
Andreas
  • 154,647
  • 11
  • 152
  • 247
0

To get the number of milliseconds within the second, you can do localTime.get(ChronoField.MILLI_OF_SECOND).

You can do localTime.toNanoOfDay() / 1000000 to get the number of milliseconds since the start of the day.

Since a LocalTime has no day or date associated with it, you can't directly get milliseconds-since-the-epoch.

You can attach a LocalDate to a LocalTime using LocalTime.atDate to get a LocalDateTime. Then you can call atZone, atOffset or toInstant to get an object that can return epochMillis.

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
  • Question says nothing about wanting an *epoch* value. Since input is a "lap time", that wouldn't make any sense anyway. So: -1 for that. --- Other parts of answer is good, though calling `getLong(ChronoField.MILLI_OF_DAY)` would probably be better than `toNanoOfDay() / 1000000`. Anyway: +1 for that. --- So no vote either way from me. – Andreas Dec 23 '19 at 07:30
  • @Andreas You're giving me a -1 for giving a complete answer ? I'll remember that – Erwin Bolwidt Dec 23 '19 at 12:30
  • The reason for my virtual down-vote was you entirely ignored an important part of the question, i.e. that the time was a "lap time". A "complete" answer would be to tell OP not to misuse `LocalTime` for a "lap time" value, but use the more appropriate `Duration` class instead, because a lap time *is* a duration, not a time of day. – Andreas Dec 23 '19 at 22:09