LocalTime 方法返回没有意义的整数

LocalTime methods returning integers that do not make sense

我有这个函数,我应该写 returns 一个 Collection<String>。它有 3 个参数:LocalTime startDateLocalTime endDateCollection<String> logLines。我应该编写函数体,以便它成功地从日志中找到并删除不在 startDateendDate 定义的特定时间范围内的任何行,然后是 returns 该日志。

计划是使用 LocalTime 方法 getHour()getMinute() 和 [从 endDatestartDate 获取小时、分钟和秒=24=],并转换为秒,以查看在时间范围开始时经过了多少秒以及在时间范围结束时经过了多少秒。然后我遍历 logLines 中的所有字符串。它们的格式为:2012-05-11T02:11:44Z This program did this operation successfully.。我拆分每个字符串以获取该行的第一部分,并使用 ISO_DATE_TIME 格式的 LocalTime.parse() 对其进行解析以获取日期并将其存储在 LocalTime logDate 中。最后,我使用上面列出的相同 LocalTime 方法来获取到此日志时间所用的总秒数。

我遇到的问题是,当我使用 LocalTime 方法时,无论 startDateendDatelogDate是。在此之前,我从未使用过 Java 8 或任何 Java 库,所以我想不出它为什么或如何这样做。我知道当传递 startDateendDate 时,它们也是与使用 LocalDate.parse()ISO_DATE_TIME 格式解析的日志日期格式相同的字符串。

public Collection<String> search(Collection<String> logLines, LocalTime startDate, LocalTime endDate) {

    // format date extracted from logLines to same format as startDate and endDate 
    DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;

    // total seconds elapsed at startDate
    // total seconds elapsed by endDate
    int startSeconds = startDate.getHour() * 60 * 60;     
    startSeconds    += startDate.getMinute() * 60; 
    startSeconds    += startDate.getSecond();

    int endSeconds = endDate.getHour() * 60 * 60;
    endSeconds    += endDate.getMinute() * 60;
    endSeconds    += endDate.getSecond();

    // iterate through logLines
    int i = 0;
    for(String logLine : logLines) {            

        // array of strings to separate logLine into parts
        String[] line = new String[2];
        line          = logLine.split("\t");

        // localTime object to store extracted date from logLines
        LocalTime logDate = LocalTime.parse(line[0], formatter);             

        // get seconds elapsed from logDate
        int logDateSeconds = logDate.getHour() * 60 * 60;
        logDateSeconds    += logDate.getMinute() * 60;
        logDateSeconds    += logDate.getSecond();

        // print amount of seconds to console
        System.out.print(logDateSeconds);
        System.out.print(" ");
        System.out.print(startSeconds);
        System.out.print(" ");
        System.out.print(endSeconds);
        System.out.print("\n");

        if (logDateSeconds > endSeconds || logDateSeconds < startSeconds) {

            logLines.remove(i);
            i--;

        }

        i++;

    }

    return logLines;

}

当我将 startSecondsendSecondslogDateSeconds 打印到控制台时,我得到

12114 12116 12122
12114 12116 12122
12114 12116 12122
12114 12116 12122
12114 12116 12122
12114 12116 12122
12114 12116 12122
12114 12116 12122

无论输入是什么。

请指出正确的方向。

示例输入:

logLines 包含

2012-05-11T02:11:44Z    This program did this operation successfully.
2012-05-11T02:12:52Z    This program did this operation successfully.
2012-05-11T02:14:17Z    This program did this operation successfully.
2012-05-11T02:18:02Z    This program did this operation successfully.
2012-05-11T02:20:30Z    This program did this operation successfully.

startDate = 02:11:44endDate = 02:19:57

我最初的问题的答案是我使用 for(String logLine : logLines) 迭代 logLines 的方式。我把它改成了for(Iterator<String> logLine = logLines.iterator(); logLine.hasNext(); )。这要感谢 on how I was using logLines.remove(i). Before coming to that conclusion I noticed that the dates in logLines were instants and I attempted to use DateTimeFormatter.ISO_INSTANT, but LocalTime could not parse the time, which makes sense. I also tried creating LocalDateTime, ZonedDateTime, and Instant objects, but they all returned the error Cannot find symbol. I don't know why this is exactly because this is my first run-around with Java 8, and Java in general. The only classes that were made available to me to manipulate time were java.time.LocalTime and java.time.DateTimeFormatter. InstantLocalTimeZonedLocalTime 在同一个包中,我认为相同,但显然那些 class 不是在这个项目中我可以访问。我也尝试使用 isBefore()isAfter()equals(),但它们没有像我预期的那样工作,我认为如果我可以计算经过的秒数就没有必要了。

然后我 运行 进入另一个问题,我收到 Exception in thread "main" java.time.format 错误。这很有趣,因为我看不到测试用例的输入或输出,而且我不知道 Exception 是什么。我认为问题是 logLine.next() 不包含日期或时间。为了解决这个问题,我摸索了 trycatch 块,尝试使用 DateTimeParseException 但没有成功。我得到了和以前一样的 Cannot find symbol 错误。然后我意识到必须有一个通用的 Exception class 并且解决了这个问题。一切正常。

这是更正后的代码,它更小也更容易阅读:

public Collection<String> search(Collection<String> logLines, LocalTime startDate, LocalTime endDate) {

    // DateTimeFormatter same as startDate and endDate
    DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;

    // total seconds elapsed at startDate
    // total seconds elapsed at endDate
    int startSeconds = startDate.toSecondOfDay();
    int endSeconds   = endDate.toSecondOfDay();

    // iterate through logLines
    for(Iterator<String> logLine = logLines.iterator(); logLine.hasNext(); ) {

        // split logLine into two strings: logDate, logDescription
        // store in string array `line`
        String[] line = logLine.next().split("\t");

        // int for storing amount of seconds from logDate
        int logDateSeconds;

        // try to parse logDate
        // catch generic exception to avoid parsing error
        try {

            // parse logDate using ISO_DATE_TIME formatter
            // get amount of seconds to compare to startSeconds and endSeconds
            logDateSeconds = LocalTime.parse(line[0], formatter).toSecondOfDay();

        } catch (Exception e) {

            // continue to next iteration for any Exception
            continue;

        }


        // compare amount of seconds to see if it is within time frame
        if (logDateSeconds >= endSeconds || logDateSeconds < startSeconds) {

            // remove iteration if it is not within time frame
            logLine.remove();
        }

    }

    // return edited 
    return logLines;

}