小时数的值 24 必须在范围 [0, 23] 内。

huangapple go评论61阅读模式
英文:

Value 24 for hourOfDay must be in the range [0,23]

问题

我有一个在我的Android应用程序中使用的REST服务,它给我一个时间,仅时间,但它返回的内容也像是

"24:38:00" 或者 "25:15:00"

所以,我需要解析这个时间并以24小时制显示出真实的结果。应该是

"00:38:00""01:15:00"

我尝试了使用以下方式

LocalTime localTime = new LocalTime(timeModel.getTimeString());

但是我现在遇到了这个错误 Cannot parse "24:38:00": Value 24 for hourOfDay must be in the range [0,23]

我该怎么解决?我只需要时间,不需要日期。

英文:

I've got a REST service that I use in my Android app that gives me a time, time only, but it returns also something like

"24:38:00" or "25:15:00"

so, what I need is to parse this time and put a real result out in 24h format. It should be

"00:38:00" and "01:15:00"

I tried use this way

LocalTime localTime = new LocalTime(timeModel.getTimeString());

but i have this error now Cannot parse "24:38:00": Value 24 for hourOfDay must be in the range [0,23]

How can I solve? I need only the time not date

答案1

得分: 4

你可以分割时间字符串,获取小时、分钟和秒,然后使用 LocalTime.of 函数:

String timeData = "25:15:00";
String[] timeParts = timeData.split(":");
LocalTime localTime = LocalTime.of(Integer.parseInt(timeParts[0]) % 24,
                                   Integer.parseInt(timeParts[1]), 
                                   Integer.parseInt(timeParts[2]));
英文:

You can split time string and get hour, minute, second and then use LocalTime.of

String timeData = "25:15:00";
String[] timeParts = timeData.split(":");
LocalTime localTime = LocalTime.of(Integer.parseInt(timeParts[0]) % 24,
                                   Integer.parseInt(timeParts[1]), 
                                   Integer.parseInt(timeParts[2]));

答案2

得分: 1

你应该真的修复一下这台服务器。不过如果这是不可能的,你就必须使用类似以下代码来绕过这个 bug:

String fixServerTime(String time) {
  if (time.startsWith("24")) {
    return "00" + time.substring(2);
  } else {
    return time;
  }
}

// 在其他地方
LocalTime localTime = new LocalTime(fixServerTime(timeModel.getTimeString()));
英文:

You should really fix this server. If this is impossible though, you'll have to work around this bug with something like

String fixServerTime(String time) {
  if (time.startsWith("24")) {
    return "00" + time.substring(2)
  } else {
    return time
  }
)

// elsewhere
LocalTime localTime = new LocalTime(fixServerTime(timeModel.getTimeString()));

答案3

得分: 1

在这种情况下,您可以假设这些时间字符串不遵循任何规则,因为如果“24”和“25”作为“hour”部分的值存在,那么为什么它们不会回复“37”呢?

您可以使用以下代码来进行清理:

private static String modifyTimeString(String s) {
  // 小时可能只有1位数字,所以“前2个字符”不是安全的方法
  int colonIndex = s.indexOf(':');
  String hoursString = s.substring(0, colonIndex);
  Integer hours = Integer.valueOf(hoursString);
  if (hours < 24) {
    return s;
  }
  /*while (hours >= 24) {
    hours -= 24;
  }*/
  // 更智能的做法,参见ronos的回答:
  hours = hours % 24;
  // 对于单位数字的小时,在前面加上0
  hoursString = hours.toString();
  if (hours < 10) {
    hoursString = "0" + hoursString;
  }
  return hoursString + s.substring(colonIndex);
}

调用 modifyTimeString("25:15:00") 返回 "01:15:00"

英文:

In this case you could assume that these time Strings are not following any rules, because if 24 and 25 exist as values for the "hour" part, why wouldn't they reply with 37?

You can use this to clean this up:

private static String modifyTimeString(String s) {
  //the hour might only be 1 digit, so &quot;the first 2 chars&quot; is not a safe approach
  int colonIndex = s.indexOf(&#39;:&#39;);
  String hoursString = s.substring(0, colonIndex);
  Integer hours = Integer.valueOf(hoursString);
  if(hours &lt; 24) {
    return s;
  }
  /*while(hours &gt;= 24) {
    hours -= 24;
  }*/
  //smarter, see ronos answer:
  hours = hours % 24;
  //put a leading 0 in there for single-digit-hours
  hoursString = hours.toString();
  if(hours&lt;10) {
    hoursString = &quot;0&quot; + hoursString;
  }
  return hoursString + s.substring(colonIndex);
}

Calling modifyTimeString(&quot;25:15:00&quot;) returns &quot;01:15:00&quot;

答案4

得分: 1

## 使用宽松解析器

您需要的是一个宽松的格式化程序来进行解析 - 更准确地说,是一个具有宽松解析样式的格式化程序(格式化程序还具有解析样式,在您的情况下不需要是宽松的)。

```java
DateTimeFormatter timeFormatter = DateTimeFormatter.ISO_LOCAL_TIME
        .withResolverStyle(ResolverStyle.LENIENT);
String timeString = "25:15:00";

LocalTime time = LocalTime.parse(timeString, timeFormatter);
System.out.println(time);

输出:

> 01:15

您不需要手动解析,也不需要任何 if 语句,也不需要取模运算。您不需要处理任何特殊情况。所有这些都可以交给标准库。这对于代码的可读性和对代码的信任都是有好处的。


<details>
<summary>英文:</summary>

## Use a lenient parser

What you need is a leneient formatter for parsing — or more precisely, a formatter with lenient resolver style (a formatter also has a parse style, it doesn’t need to be lenient in your case).

		DateTimeFormatter timeFormatter = DateTimeFormatter.ISO_LOCAL_TIME
				.withResolverStyle(ResolverStyle.LENIENT);
		String timeString = &quot;25:15:00&quot;;
		
		LocalTime time = LocalTime.parse(timeString, timeFormatter);
		System.out.println(time);

Output:

&gt; 01:15

You don’t need any hand parsing, nor any `if` statement, nor any modulo operation. You don’t need to handle any special cases. You can leave it all to the standard library. Which is good both for readability and for trust in the code.

</details>



huangapple
  • 本文由 发表于 2020年9月22日 16:24:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64005792.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定