gpt4 book ai didi

java - 如何解析HH :mm:ss without hours?

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

我有一个格式如下的字符串:“2h 33m 50s”。如果小时数达到零,则该字符串将更改为:“33m 50s”。因此,如果该字符串经过 LocalTime.parse ,则会抛出异常。我该如何解析它?

fun main() {
val timeString = "2h 33m 50s"
val timeString2 = "33m 50s"
val formatterH = DateTimeFormatter.ofPattern("[H]'h 'm[m]'m 's[s]'s'")
val formatterM = DateTimeFormatter.ofPattern("m[m]'m 's[s]'s'")

val time = LocalTime.parse(timeString, formatterH)
println(time)
val time2 = LocalTime.parse(timeString2, formatterH) //throws exception
println(time2)
val time3 = LocalTime.parse(timeString2, formatterM) //throws similar exception
println(time3)
}
Program output:
02:33:50
Exception in thread "main" org.threeten.bp.format.DateTimeParseException: Text '33m 50s' could not be parsed: Unable to obtain LocalTime from TemporalAccessor: DateTimeBuilder[fields={SecondOfMinute=50, NanoOfSecond=0, MicroOfSecond=0, MinuteOfHour=33, MilliOfSecond=0}, ISO, null, null, null], type org.threeten.bp.format.DateTimeBuilder
at org.threeten.bp.format.DateTimeFormatter.createError(DateTimeFormatter.java:1559)
at org.threeten.bp.format.DateTimeFormatter.parse(DateTimeFormatter.java:1496)
at org.threeten.bp.LocalTime.parse(LocalTime.java:437)
at MainKt.main(main.kt:16)
at MainKt.main(main.kt)
Caused by: org.threeten.bp.DateTimeException: Unable to obtain LocalTime from TemporalAccessor: DateTimeBuilder[fields={SecondOfMinute=50, NanoOfSecond=0, MicroOfSecond=0, MinuteOfHour=33, MilliOfSecond=0}, ISO, null, null, null], type org.threeten.bp.format.DateTimeBuilder
at org.threeten.bp.LocalTime.from(LocalTime.java:405)
at org.threeten.bp.LocalTime$1.queryFrom(LocalTime.java:116)
at org.threeten.bp.LocalTime$1.queryFrom(LocalTime.java:113)
at org.threeten.bp.format.DateTimeBuilder.build(DateTimeBuilder.java:642)
at org.threeten.bp.format.DateTimeFormatter.parse(DateTimeFormatter.java:1492)

最佳答案

tl;博士

你工作太努力了,而且方向错误。

正则表达式对于这个问题来说太过分了。

不需要 DateTimeFormatter类,也不是格式化模式。

使用Duration解析 ISO 8601 的类根据您的输入制作的字符串。

Duration                    // Represent a span-of-time not attached to the timeline with class `Duration`, not `LocalTime`.
.parse( // By default, the *java.time* classes such as `Duration` use the standard ISO 8601 formats to parse/generate date-time strings.
"PT" + "2h 33m 50s" // Morph your input string to comply with the ISO 8601 standard. Add `P` for the beginning, and `T` to separate years-months-days from hours-minutes-seconds.
.replace( " " , "" ) // Delete any SPACE characters by replacing them with nothing.
.toUpperCase() // Force all the letters to be uppercase.
) // Returns a `Duration`.

同样的几分钟和几秒钟。

Duration.parse( "PT" + "33m 50s".replace( " " , "" ).toUpperCase() ) 

持续时间,而不是LocalTime

if this string goes through LocalTime.parse

LocalTime是一天中的某个时间。您的输入不是一天中的时间。

您的输入字符串代表未附加到时间线的时间跨度。其类别是 Duration .

ISO 8601

您的输入字符串接近 standard ISO 8601格式,PnYnMnDTnHnMnSP 标志着开始。 T 将任何年-月-日与任何时-分-秒分开。

让我们调整您的输入字符串以符合标准。

String input = "PT" + "2h 33m 50s".replace( " " , "" ).toUpperCase() ;

input: PT2H33M50S

解析。

Duration d = Duration.parse( input ) ;  // PT2H33M50S

要生成标准 ISO 8601 格式的字符串,请调用 toString

String output = d.toString() ;

output: PT2H33M50S

您可以将该 Duration 对象添加到一天中的某个时间,即 LocalTime

LocalTime lt = LocalTime.NOON.plus( d ) ;

您可以将该持续时间添加到 UTC 中的当前时刻,以确定 future 的时刻(或者过去的时刻是持续时间为负值的时刻)。

Instant instant = Instant.now().plus( d ) ;

lt.toString(): 14:33:50

查看以上所有内容code run live at IdeOne.com .

您可以提取持续时间的每个部分。

long daysPart = d.toDaysPart() ;  // 24-hour chunks of time, not related to calendar days.
int hoursPart = d.toHoursPart() ;
int minutesPart = d.toMinutesPart() ;
int secondsPart = d.toSecondsPart() ;

或者您可能希望将整个时间跨度作为总毫秒数。

long millis = d.toMillis() ;  // All the hours-minutes-seconds and such totaled as one count of elapsed milliseconds.

关于java - 如何解析HH :mm:ss without hours?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56158486/

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