For one of my project I have to convert String to integer. How to convert “16:45:20” string to integer in java. I am using
int time = Integer.parseInt(string)
It’s giving me a
How to solve this?? I have also read a couple of resources on string to integer in java on wiki and Scaler Topics to refresh my understanding of the string to integer in java.
The colon is what is giving you the problem. Parse to int is intended to just that - parse a value to an integer (whole numbers). The parser doesn’t know how to handle it.
What would 16:45:20 convert to? 164520? If so, do a string replace to strip out the colons, THEN do the parse. If you’re trying to capture each number (16, 45, 20), then split the string into an array and convert each element in the array…
If you’re trying to convert it to a time, there are other parsers to do just that. Search for string to timestamp
note that most string-to-timestamp parsers with just a time string will probably assume the date to either be “today” or January 1, 1970. (The latter I would imagine is more common, because then the converted timestamp is also “X hours, Y minutes, and Z seconds in seconds”)