[Solved] Why doesn’t String.split(“:”) work correctly?


You have a problem in this part:

    if(hour.startsWith("0")) {
      tTime = hour.split("0");
      if(tTime.length > 0)  hour = tTime[0];
      else hour = "0";
    }

So if your input is “08:15”, hour value is “08”. Now when you split it using delimiter “0”, you get an array { "", "8" }. So, now tTime[0] is “”.

Use instead:

    if(hour.startsWith("0")) {
      hour = hour.substring(1);
    }

PS: There would be no problem in case the hour doesn’t start with a “0”.

6

solved Why doesn’t String.split(“:”) work correctly?