gpt4 book ai didi

java - 从字符串中获取正确的小时值

转载 作者:行者123 更新时间:2023-12-02 07:32:29 25 4
gpt4 key购买 nike

我需要从时间字符串中获取正确的小时值。这是示例代码:

String startTime = "10:00";
int hours = Integer.valueOf(startTime.substring(0,1));

问题在于,对于此示例,hours 等于 1 而不是 10。再如,使用 startTime = "01:00",则 hours 等于 0 而不是 1。如何从字符串中正确确定小时数?

最佳答案

String[] hm = startTime.split(":");

hm[0] 包含小时部分,hm[1] 包含分钟部分。

关于java - 从字符串中获取正确的小时值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8917255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com