所以我有一个日志文件,我需要搜索某些字符串并打印出属于某些日期范围的行。
我已经完成了第一部分,但我坚持第二部分。我如何在特定日期内打印出一个字符串。所以说我想在2012-09-01和2012-09-15之间打印所有字符串
日志示例:
2012-09-13 16:04:22 DEBUG SID:34523 BID:1329 RID:65d33 'Starting new session' 2012-09-13 16:04:30 DEBUG SID:34523 BID:1329 RID:54f22 'Authenticating User' 2012-09-13 16:05:30 DEBUG SID:42111 BID:319 RID:65a23 'Starting new session' 2012-09-13 16:04:50 ERROR SID:34523 BID:1329 RID:54ff3 'Missing Authentication token' 2012-09-13 16:05:31 DEBUG SID:42111 BID:319 RID:86472 'Authenticating User' 2012-09-13 16:05:31 DEBUG SID:42111 BID:319 RID:7a323 'Deleting asset with ID 543234' 2012-09-13 16:05:32 WARN SID:42111 BID:319 RID:7a323 'Invalid asset ID'
到目前为止,这是我的代码:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
public class ReadLogs {
public static void main(String args[]) throws FileNotFoundException{
String line, logString ="";
Date startDate, endDate;
ArrayList<String> logList = new ArrayList<>();
Scanner logScanner = new Scanner(new File("C:\\Users\\cmccarth\\Desktop\\logs.txt"));
while (logScanner.hasNextLine()) {
line = logScanner.nextLine();
logList.add(line);
}
for (String z : logList) {
// System.out.println(z);
}
// This function prints out all lines containing a specific string
for( int i = 0; i <= logList.size() - 1; i++)
{
logString = logList.get(i);
if(logString.contains("16:04:22")){
System.out.println("String Contains" +logString);
}
}
}
}
答案 0 :(得分:1)
执行以下步骤:
1)创建2个Date对象,其中包含您要搜索的日期范围(使用简单日期格式获取这些日期):
Date lowerRange;
Date upperRange;
2)现在循环遍历已过滤的日志列表并确认从日志文件中获取的字符串,然后将日期作为标记[0]。
String[] tokens = logString.split(" ");
String dateStr = tokens[0];
// Convert this date to String using Simple Date Format
Date date = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse(dateStr);
3)现在,您可以将此日期与您必须查看的2个日期进行比较。
if(date > lowerRange && date < upperRange)
{
// this is the date you wanted...process the log file entry
}
请参阅以下SO帖子,了解字符串到日期的转换:String-to-date
答案 1 :(得分:0)
试试这个示例代码。
正在使用Calendar
,SimpleDateFormat
和Pattern
。
Calendar
:与时间参数一起形成开始日期和结束日期SimpleDateFormat
:将字符串解析为日期对象Pattern
:从日志中获取特定的日期模式以下是代码:
String[] logs = new String[] {
"2012-09-13 16:04:22 DEBUG SID:34523 BID:1329 RID:65d33 'Starting new session'",
"2012-09-13 16:04:30 DEBUG SID:34523 BID:1329 RID:54f22 'Authenticating User'",
"2012-09-13 16:05:30 DEBUG SID:42111 BID:319 RID:65a23 'Starting new session'",
"2012-09-13 16:04:50 ERROR SID:34523 BID:1329 RID:54ff3 'Missing Authentication token'",
"2012-09-13 16:05:31 DEBUG SID:42111 BID:319 RID:86472 'Authenticating User'",
"2012-09-13 16:05:31 DEBUG SID:42111 BID:319 RID:7a323 'Deleting asset with ID 543234'",
"2012-09-13 16:05:32 WARN SID:42111 BID:319 RID:7a323 'Invalid asset ID'" };
List<String> logList = new ArrayList<String>();
for (String log : logs) {
logList.add(log);
}
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2012);
cal.set(Calendar.MONTH, 8);
cal.set(Calendar.DATE, 13);
cal.set(Calendar.HOUR_OF_DAY, 16);
cal.set(Calendar.MINUTE, 4);
cal.set(Calendar.SECOND, 30);
cal.set(Calendar.MILLISECOND, 0);
Date startDate = cal.getTime();
cal.set(Calendar.MINUTE, 5);
cal.set(Calendar.SECOND, 31);
Date endDate = cal.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Pattern pattern = Pattern.compile("^(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2})");
Matcher matcher = null;
String logString = null;
Date date = null;
for (int i = 0; i < logList.size(); i++) {
logString = logList.get(i);
matcher = pattern.matcher(logString);
if (matcher.find()) {
try {
date = format.parse(matcher.group());
if (date.getTime() >= startDate.getTime()
&& date.getTime() <= endDate.getTime()) {
System.out.println(logString);
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}
输出:(在2012-09-13 16:04:30
和2012-09-13 16:05:31
之间进行日志记录)
2012-09-13 16:04:30 DEBUG SID:34523 BID:1329 RID:54f22 'Authenticating User'
2012-09-13 16:05:30 DEBUG SID:42111 BID:319 RID:65a23 'Starting new session'
2012-09-13 16:04:50 ERROR SID:34523 BID:1329 RID:54ff3 'Missing Authentication token'
2012-09-13 16:05:31 DEBUG SID:42111 BID:319 RID:86472 'Authenticating User'
2012-09-13 16:05:31 DEBUG SID:42111 BID:319 RID:7a323 'Deleting asset with ID 543234'
答案 2 :(得分:0)
尝试一下(对我有用)
static void readdata(String startDate, String endDate) throws IOException, ParseException {
String file = "your file location";
List<String> logList = new ArrayList<String>();
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = reader.readLine();
while (line != null) {
logList.add(line);
// read next line
line = reader.readLine();
}
reader.close();
Pattern pattern = Pattern.compile("([12]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\\d|3[01]))");
Matcher matcher = null;
String logString = null;
Date date = null;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
for (int i = 0; i < logList.size(); i++) {
logString = logList.get(i);
matcher = pattern.matcher(logString);
if (matcher.find()) {
date = format.parse(matcher.group());
Date st = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse(startDate);
Date ed = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse(endDate);
int res = st.compareTo(date);
if (res <= 0) {
if (logString.contains(startDate)) {
{
System.out.println(logString);
}
continue;
}
if (date.after(ed)) {
break;
}
System.out.println(logString);
}
}
}
}