0

My question demands that the time must be entered in a format of HH:MM:SS. However, these must be separated by white spaces when the input must be taken. Now we have to operate on this piece of time given to us, so we would be needing to separate the HH MM and SS. How do I do it? how do I separate the pieces based on delimiters? I can do the rest of the operations that my question demands me to, I just need help on the delimiter part.

Sample input: 24 05 10

NewJava
  • 7
  • 4
  • 1
    Does this answer your question? [How to split a string in Java](https://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) – vicpermir Apr 02 '20 at 06:42
  • Capture users' input in `String` and `split` it with white space – AbhiN Apr 02 '20 at 06:43

2 Answers2

0

If the format is always fixed, you can split

String time = "24 12 56";
String[] timeQuantities = time.split(" ");
String hour = timeQuantities[0];
String minutes = timeQuantities[1];
String seconds = timeQuantities[2];

or

String time = "24 12 56";
int firstSpaceIndex = time.indexOf(" ");
int secondSpaceIndex = time.indexOf(" ", firstSpaceIndex+1);
String hour = time.substring(0,firstSpaceIndex);
String minutes = time.substring(firstSpaceIndex+1, secondSpaceIndex);
String seconds = time.substring(secondSpaceIndex);
Nandu Raj
  • 2,072
  • 9
  • 20
0
SimpleDateFormat format = new SimpleDateFormat("HH mm ss");

Date   date       = format.parse ( "24 05 10" );    
Nicolae Natea
  • 1,185
  • 9
  • 14