이 함수는 내가 작성해야하는데 Collection<String>
을 반환합니다. 그것에는 LocalTime startDate
, LocalTime endDate
및 Collection<String> logLines
의 3 개의 매개 변수가 있습니다. 함수 본문을 작성하여 startDate
및 endDate
에 정의 된 특정 기간 내에 있지 않은 행을 찾아서 제거한 다음 해당 로그를 반환합니다.의미가없는 정수를 반환하는 LocalTime 메서드
계획은 경과 시간 프레임의 시작과 얼마나 많은에 경과하는 시간 (초)을 볼 수 LocalTime
방법 getHour()
, getMinute()
및 getSecond()
를 사용하여 endDate
및 startDate
에서시, 분, 초를 얻을, 초로 변환하는 것입니다 시간의 끝. 그런 다음 모든 문자열을 반복하여 logLines
에 반복합니다. 형식은 2012-05-11T02:11:44Z This program did this operation successfully.
입니다. 각 문자열을 분할하여 줄의 첫 번째 부분을 가져오고 형식의 LocalTime.parse()
형식을 사용하여 구문 분석하고 날짜를 얻고 LocalTime logDate
에 저장합니다. 마지막으로 위에 나열된 것과 동일한 LocalTime
메서드를 사용하여이 로그 시간까지 경과 한 총 시간 (초)을 구합니다.
오전 데 문제가 있음을 나는 또 다시 상관없이 startDate
, endDate
, 또는 logDate
가 무엇인지 같은 모든 숫자를 저장 년대 LocalTime
방법을 사용하는 경우. 필자는 Java 8 또는 Java 라이브러리를 사용한 적이 없기 때문에이 작업을 수행하는 이유 또는 방법을 생각할 수 없습니다. startDate
과 endDate
을 전달하면 LocalDate.parse()
및 ISO_DATE_TIME
형식을 사용하여 분석 한 로그 날짜와 동일한 형식의 문자열이기도합니다. 내가 콘솔에 startSeconds
, endSeconds
및 logDateSeconds
을 인쇄 할 때
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;
}
내가 상관없이 입력이 무엇인지
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
및 endDate = 02:19:57
당신은 아무 이유없이 새로운 배열을 만드는 : 여기
읽기가 훨씬 작고 쉽게 수정 된 코드입니다. 그냥'String [] line = logLine.split ("\ t");'을 수행하십시오. – shmosel그래서'startSeconds'와'endSeconds'는 변경되지 않으므로 코드의 유일한 부분은'LocalTime logDate = LocalTime.parse (line [0], formatter);'- 입력 내용은 무엇입니까? –
그 방법은 이전에 해보았지만, 내가 그걸 바꾸면 문제가 생겼는지는 몰랐습니다. – kohai