Time Formatting

Asked

Viewed 32 times

0

I am making an application that divided a String to write the same in time format. But if I put 102330 instead of returning me 10:23:30 it brings me 1:02:33 and if I put 112126 it formats correctly to 11:21:26. If anyone can help me, I’d appreciate it.

StringBuilder format = new StringBuilder();
                                    format.append(hora.substring(0, 2)).
                                            append(":").append(hora.substring(2, 4)).
                                            append(":").append(hora.substring(4, 6));
                                    System.out.println("Hora: " +format);

1 answer

2


The code is correct, there is probably a space at the beginning of the text 102330.

Try debugging the function or do a simple sysout with the original value to see the space.

  • That’s exactly what it was. I used a Pattern to clear the spaces and it worked normally

Browser other questions tagged

You are not signed in. Login or sign up in order to post.