1

I am trying to get the total minutes of a Local Time Object in my jtable. As I didnt find any method I tried to convert it to a string and then convert the string to minutes.

The lenght of the nanoseconds is always changing depending of the table inputs and throws then a DateTimeParseException.

How can I convert it without parse Exception ?

Here is the code I use:

String pause = String.valueOf(table.getValueAt(zeile, 4));
DateTimeFormatter formatter  = DateTimeFormatter.ofPattern("HH:mm:ss.SSSSSS");
LocalTime localTime1 = LocalTime.parse(pause, formatter);

Throws the following error:

Exception in thread "AWT-EventQueue-0" java.time.format.DateTimeParseException: Text '00:21:07.480781500' could not be parsed, unparsed text found at index 15
Guen
  • 23
  • 2
K. Wenzel
  • 13
  • 1
  • 7
  • 1
    Your formatter only has microseconds. Try `HH:mm:ss.SSSSSSSSS`. – shmosel Mar 16 '18 at 23:26
  • If I understand correctly, you are taking a `LocalTime` object out of your `JTable` cell, converting it to `String` through `String.valueOf` and then parsing ot back into a `LocalTime`. Is that getting you anywhere? – Ole V.V. Mar 17 '18 at 11:33
  • 00:21:07.480781500, is that an amount of time, a duration? Or is it a time of day, about 21 minutes past midnight? – Ole V.V. Mar 17 '18 at 11:37

4 Answers4

2

With this format, you don't need a formatter, just do:

LocalTime localTime1 = LocalTime.parse(pause);

All java.time classes can directly parse a string if it's in ISO8601 format, which is the case: https://en.m.wikipedia.org/wiki/ISO_8601

Guen
  • 23
  • 2
0

Try

    DateTimeFormatter formatter  = DateTimeFormatter.ofPattern("HH:mm:ss.SSSSSSSSS");

The time passed to the parse method has 9 characters for the milliseconds but your format string only specified 6.

Dusty
  • 71
  • 2
  • The OP talks about varying lengths of input, so your fixed pattern would not always work. A builder-approach specifying minimum and maximum counts of nanosecond digits is more helpful. – Meno Hochschild Mar 17 '18 at 04:00
0

When parsing the string the Format is not diferent from the value I get with String.valueOf()-Method.

I managed to get the minutes like this:

String pause = String.valueOf(table.getValueAt(zeile, 4));
if (pause.length()>5) {
        pause = pause.substring(0,pause.indexOf(':') + 3);
}
String[] split = pause.split(":");
int hours = Integer.parseInt(split[0]);
int minutes = Integer.parseInt(split[1]);
int result = hours * 60 + minutes;
System.out.println("result: " + result);

Is there a more elegant way to do this or is this correct ?

K. Wenzel
  • 13
  • 1
  • 7
0

I suspect that your 00:21:07.480781500 is really the length of your break or pause, so a duration, an amount of time. If so, you are really using the wrong class in your table cell.

  • A Duration as for a duration, an amount of time, like 21 minutes and some seconds.
  • A LocalTime is for a time of day from 00:00:00 through 23:59:59.999999999.

If I understand what you mean, getting the total minutes from a duration makes sense, getting them from a LocalTime not really.

The challenge with a Duration is formatting it nicely for the user. However this has been addressed in a number of questions and answers already. So you may find a solution in this question: How to format a duration in java? (e.g format H:MM:SS) and/or search for other questions and answers. I think you need to subclass DefaultTableCellRenderer to display the formatted Duration in your table. In return getting the minutes is straightforward:

    Duration pause = (Duration) table.getValueAt(zeile, 4);
    long result = pause.toMinutes();
    System.out.println("result: " + result);

This printed:

result: 21

If you really meant a time of day, you should of course keep your LocalTime objects. Then the answer is:

    LocalTime pause = (LocalTime) table.getValueAt(zeile, 4);
    int result = pause.get(ChronoField.MINUTE_OF_DAY);
    System.out.println("result: " + result);

The output is the same:

result: 21

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Thanks a lot for this explanation...I have in fact two times of a day (beginning time and end time) and the cell where the user can put the duration of the pause he made. As used also LocalTime for the pause because in my jtable i use LGoodDatePicker and by default it works with LocalTime. I guess the output of LGoodDatePicker can be changed to a Duration but I didn't look into it. But thanks for your comments....helped me a lot ! – K. Wenzel Mar 17 '18 at 14:30
  • So for calculating the duration from start to end and subtracting the break (pause) you would need something like `Duration.between.(startTime, endTime).minus(pause)` (where `pause` is a `Duration`). To convert a `LocalTime` that should have been a `Duration`, use `Duration.between(LocalTime.MIDNIGHT, localTimeToBeConverted)`. – Ole V.V. Mar 17 '18 at 14:52