在Java中,可以使用`SimpleDateFormat`类来将时间字符串转换为秒数。以下是一个示例代码: ```java import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Main { public static void main(String[] args) { String timeString = "12:34:56"; SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); try { Date date = sdf.parse(timeString); long seconds = date.getTime() / 1000; System.out.println("Seconds: " + seconds); } catch (ParseException e) { e.printStackTrace(); } } } ``` 在上面的代码中,我们首先定义了一个时间字符串`timeString`,表示为"HH:MM:SS"格式。然后,我们创建了一个`SimpleDateFormat`对象`sdf`,并指定了时间格式为"HH:mm:ss"。接下来,我们使用`sdf.parse()`方法将时间字符串解析为`Date`对象。最后,我们通过`date.getTime()`方法获取毫秒数,并将其除以1000得到秒数。 运行上述代码,将输出: ``` Seconds: 45296 ``` 表示时间字符串"12:34:56"对应的秒数为45296秒。