LocalDateTime不起作用 - 没有任何类型的错误
问题描述:
我正在开发一个Java项目,一个日志分析器,它以通用的方式导入任何类型的日志信息。 实际上,我将日志类实例中的任何日志表和事件类实例中的任何表单行分开。 问题是:日志读取所有表单,并将每个读取行的实例实例化,并将其放置在LinkedHashSet中。 当事件被构造时,它取决于它所处的Log类型 - 所有信息(例如事件发生的时间)。 除了从事件字符串获取日期/时间外,一切都完美无缺。LocalDateTime不起作用 - 没有任何类型的错误
事实上,没有任何一种错误,Log的添加事件循环停止。 删除每一个时间相关的行,我正确地设法得到一切:与他们,它停止,没有任何错误,但让我只得到一堆线,而不是我应该得到的数千。
在这里你可以找到日志和事件类,甚至从事件和事件的最后一行的管理来获得时间和不第一个获得时间的方法。
日志:
public class Log {
/** LogType assigned to log file */
private LogType type;
/** filename associated to log file*/
private String name;
/** path associated to log file */
private String path;
private LinkedHashSet<Evento> events;
/**
* Log constructor:
* @param path points to file which create log instance from
* @param type is the LogType type associated with the rising Log
* @param bInitialize indicates if Log has to initialize events list
*/
public Log(String path, LogType type, boolean bInitialize) {
String[] pathComponents = path.split("/");
this.path = path;
this.type = type;
this.name = pathComponents[pathComponents.length - 1];
populateEvents();
}
public LinkedHashSet<Evento> getEvents() {
return events;
}
/** type field getter */
public LogType getType() {
return type;
}
/** name field getter */
public String getName() {
return name;
}
/** path field getter */
public String getPath() {
return path;
}
/** @return path field */
public String toString() {
return path;
}
public TreeSet<Utente> getUsers() {
TreeSet<Utente> users = new TreeSet<Utente>();
for (Evento event : events)
users.add(event.getUser());
return users;
}
private void populateEvents() {
events = new LinkedHashSet<Evento>();
try {
File file = new File(path);
FileInputStream fInputStream = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
fInputStream.read(data);
fInputStream.close();
String[] eventsRead = new String(data, "UTF-8").split("\n");
for (String event : eventsRead)
events.add(new Evento(event, type));
} catch (Exception e) {
// Nothing really needed.
}
}
/** name field setter */
public void setName(String name) {
this.name = name;
}
/**
* Requests that the file or directory denoted by this abstract
* pathname be deleted when the virtual machine terminates.
*/
public void setToDeleted() {
new File(path).deleteOnExit();
}
}
事件:
public class Evento implements Comparable<Evento> {
private LocalDateTime time;
private LogType type;
private String event;
private Utente user;
public Evento(String event, LogType type) {
this.event = event;
this.type = type;
time = type.getAssociatedLoader().getTimeFromLine(event);
user = type.getAssociatedLoader().getUserFromLine(event);
}
public boolean equals(Evento comparedEvent) {
return event.equals(comparedEvent.getEvent());
}
@Override
public int compareTo(Evento comparedEvent) {
return event.compareTo(comparedEvent.getEvent());
}
public LocalDateTime getTime() {
return time;
}
public String getEvent() {
return event;
}
public Utente getUser() {
return user;
}
@Override
public String toString() {
return event;
}
}
getTimeFromLine()方法:
@Override
public LocalDateTime getTimeFromLine(String line) {
String clockString = line.split("\t")[2];
return LocalDateTime.of(Integer.parseInt(clockString.substring(0,4)),
Integer.parseInt(clockString.substring(6,7)),
Integer.parseInt(clockString.substring(9,10)),
Integer.parseInt(clockString.substring(11,13)),
Integer.parseInt(clockString.substring(15,16)),
Integer.parseInt(clockString.substring(17,19)));
}
线示例(第一正常工作,而不是后者):
142\twestchester.gov\t2006-03-20 03:55:57\t1\thttp://www.westchestergov.com
142\tspace.comhttp\t2006-03-24 20:51:24\t\t
答
-
你不应该吞下异常 - 如果你让它传播,你会得到一个相当自我解释的异常消息,可以帮助你找到问题!
java.time.DateTimeException:为DAYOFMONTH值无效(有效值1 - 28/31):0
所以一个你
clockString.substring()
不使用权指数 你
getTimeFromLine
方法是不必要的复杂,我会建议使用DateTimeFormatter
,而不是手动解析字符串
建议更换:
private static final DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
public LocalDateTime getTimeFromLine(String line) {
String clockString = line.split("\t")[2];
return LocalDateTime.parse(clockString, fmt);
}
//在您的接收中没有任何真正需要的东西?我不会那么说。 – RealSkeptic
这里的代码太多了。请学习展示一个最小的完整示例,[MCVE](http://*.com/help/mcve)。形成这样一个例子会回答你自己的问题,解决你的问题。 –