Reputation: 29
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
Upvotes: 1
Views: 7628
Reputation: 86324
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.
Duration
as for a duration, an amount of time, like 21 minutes and some seconds.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
Upvotes: 0
Reputation: 29
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 ?
Upvotes: 0
Reputation: 23
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
Upvotes: 2
Reputation: 81
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.
Upvotes: 0