简体   繁体   English

用leap秒来解析持续时间,例如00:00:60

[英]Parse time duration with leap second like 00:00:60

How can I parse a time duration with leap second to seconds? 如何解析秒到秒的持续时间?

Example, we are receiving from another service the following duration 00:00:60 which means 1 minute but Java 8 DateTimeFormatter.ofPattern("HH:mm:ss[.SSS]") throws 例如,我们从另一个服务接收以下持续时间00:00:60,这意味着1分钟,但是Java 8 DateTimeFormatter.ofPattern("HH:mm:ss[.SSS]")抛出

java.time.format.DateTimeParseException: Text '00:00:60' could not be parsed: Invalid value for SecondOfMinute (valid values 0 - 59): 60

Which makes sense because there is no notion of 60 seconds in the ISO standard used by Java. 这是有道理的,因为Java使用的ISO标准中没有60秒的概念。

I would like to avoid using String::replace (as suggested in other SO answers to similar case) and the use of external library if possible. 我想避免使用String::replace (在类似情况的其他SO答案中建议),并尽可能避免使用外部库。

This is the current code that is throwing the exception: 这是引发异常的当前代码:

private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss[.SSS]");

public static Integer parseToSeconds(String durationText) {

    return LocalTime.parse(durationText, TIME_FORMATTER).toSecondOfDay();
}

This is how I solved the issue: 这是我解决问题的方法:

private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss[.SSS]");

public static long parsesToSeconds(String durationText) {
    Duration timeSum = Duration.ZERO;
    // Reformat to ISO 8601
    durationText = durationText.replaceFirst("(\\d{2}):(\\d{2}):(\\d{2}(?:\\.\\d+)?)", "PT$1H$2M$3S");

    timeSum = timeSum.plus(Duration.parse(durationText));

    return timeSum.getSeconds();
}

Credits to Ole VV with his answer here: https://stackoverflow.com/a/52315938/2240409 归功于Ole VV及其在这里的回答: https : //stackoverflow.com/a/52315938/2240409

This is what we have lenient resolver style for. 这就是我们宽大的解析器风格。

    String durationText = "00:00:60";
    DateTimeFormatter lenientTimeFormatter = DateTimeFormatter.ISO_LOCAL_TIME
            .withResolverStyle(ResolverStyle.LENIENT);
    int seconds = LocalTime.parse(durationText, lenientTimeFormatter).toSecondOfDay();
    System.out.println(seconds);

The output from this snippet is: 该代码段的输出为:

60 60

If your 60 seconds are really a duration, not a time of day, I don't really like it. 如果您的60秒确实是持续时间而不是一天中的某个时间,那么我就不太喜欢它。 Then I'd rather parse into a Duration as you do in your own answer. 然后,我宁愿像您自己回答那样将其解析为Duration I know that String.replace with a hard-to-read regular expression isn't a very good solution either, but I still think I'd prefer it over pretending that a duration is a time of day if this is downright wrong. 我知道带有难以理解的正则表达式的String.replace也不是一个很好的解决方案,但是我仍然认为我更喜欢它,而不是假装持续时间是一天中的某个时间,如果这是完全错误的。 I just thought I'd present the option anyway and let you make your own pick based on your reality and preferences. 我只是以为无论如何我都会提出这个选择,让您根据自己的实际情况和偏好做出自己的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM