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

时间:2016-11-08 02:01:33

标签: java java-8 java-time

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

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

我遇到的问题是,当我使用LocalTime方法时,无论LocalTimestartDate是什么,它都会一遍又一遍地存储所有相同的数字,或endDate是。在此之前我从未使用过Java 8或任何Java库,因此我无法想到为什么或如何做到这一点。我知道,当传递logDatestartDate时,它们也是与使用endDateLocalDate.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; } startSecondsendSeconds到控制台时我得到了

logDateSeconds

无论输入是什么。

请指出正确的方向。

示例输入:

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:44

1 个答案:

答案 0 :(得分:1)

我最初的问题的答案就是我使用for(String logLine : logLines)迭代logLines的方式。我将其更改为for(Iterator<String> logLine = logLines.iterator(); logLine.hasNext(); )。这要归功于shmosel's comment我如何使用logLines.remove(i)。在得出结论之前,我注意到logLines中的日期是时刻,我尝试使用DateTimeFormatter.ISO_INSTANT,但LocalTime无法解析时间,这是有道理的。我还尝试创建LocalDateTimeZonedDateTimeInstant个对象,但它们都返回了错误Cannot find symbol。我不知道为什么这是因为这是我第一次使用Java 8和Java。我可以操作时间的唯一课程是java.time.LocalTimejava.time.DateTimeFormatterInstant LocalTimeZonedLocalTimeisBefore()位于同一个包中,我认为相同,但很明显,我在这个项目中无法访问这些类。我还尝试使用isAfter()equals()Exception in thread "main" java.time.format,但它们没有按照我的预期运行,我认为如果我可以计算经过的秒数就没有必要了。

然后我遇到另一个问题,我收到Exception错误。这很有趣,因为我看不到测试用例的输入或输出,我不知道logLine.next()是什么。我认为问题是try没有包含日期​​或时间。为了解决这个问题,我遇到了catchDateTimeParseException块,尝试使用Cannot find symbol但没有成功。我得到了与以前相同的Exception错误。然后我意识到必须有一个通用的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; } 类并修复了这个问题。一切都按预期工作。

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

id | city   |  age   | gender
1  | London |   Y    |   M
2  | Milan  |   Y    |   F
3  | London |   O    |   M
4  | London |   O    |   F