字符串到整数皈依整数、字符串

2023-09-04 07:39:04 作者:回首,已陌然

有关我的项目之一,我必须将字符串转换为整数。如何转换16时45分二十秒字符串中的Java为整数。我使用

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)

它给了我 NumberFormatException异常错误。如何解决这个

It's giving me NumberFormatException error. How to solve this

感谢

推荐答案

你想它返回?几秒钟的时间跨度是多少?或者,这是24H格式的时间呢?你希望从你的例子输入字符串得到什么整型?如果你只是想的数字字符作为一个整数,然后是的,你需要分割字符串的冒号,然后将它连接,然后解析INT。

What do you want it to return? The number of seconds in the timespan? Or is this a time in 24H format? What integer do you expect to get from your example input string? If you just want the numeric characters as an integer, then yeah, you will need to split the string on the colons and then concatenate it and then parse the int.