2014-04-14 212 views
0

我试图在两个日期范围之间打印日志条目,但这样做有点麻烦。如何在文件的日期范围之间打印日期

我已经从文件中读取输入并存储得到了信息,我想为日期变量下的,但我仍然可以找出为什么它不会在范围

这里显示的日期是在文件

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' 
2012-09-14 16:04:22 DEBUG SID:34523 BID:1329 RID:65d33 'Starting new session' 
2012-09-14 16:04:30 DEBUG SID:34523 BID:1329 RID:54f22 'Authenticating User' 
2012-09-14 16:05:30 DEBUG SID:42111 BID:319 RID:65a23 'Starting new session' 
2012-09-14 16:04:50 ERROR SID:34523 BID:1329 RID:54ff3 'Missing Authentication token' 
2012-09-14 16:05:31 DEBUG SID:42111 BID:319 RID:86472 'Authenticating User' 
2012-09-14 16:05:31 DEBUG SID:42111 BID:319 RID:7a323 'Deleting asset with ID 543234' 
2012-09-14 16:05:32 WARN SID:42111 BID:319 RID:7a323 'Invalid asset ID' 

这里是我的代码

import java.io.File; 
import java.io.FileNotFoundException; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Date; 
import java.util.Locale; 
import java.util.Scanner; 

public class ReadLogs { 
private static String line, logLevel = "16:05:31", businessID ="1329", sessionID ="34523", 
     startDateStr = "2012-09-10",endDateStr ="2012-09-14" ; 
private static String logString =""; 
private static ArrayList<String> logList = new ArrayList<>(); 

public static void main(String args[]){ 
    readFile(); 
    returnLogLinesByLevel(); 
    returnLogLinesByBusiness(); 
    returnLogLinesBySessionID(); 
    returnLogLinesByDate(); 
}//close main 

/* 
* This method checks the logs for entries between a date range. 
* @param String dateToValidate - This is the date to search the logs for 
* @param String startDate - This is the starting date point 
* @param String endDate - This is the end date point 
* */ 
private static void returnLogLinesByDate() { 
    System.out.println("///////By Date/////////////////"); 
    for(int i = 0; i <= logList.size() - 1; i++) 
     { 
      logString = logList.get(i); 
      if(isThisDateWithinRange(logString.substring(0, 11))){ 
       System.out.println(logString); 
      } 
     } 
    System.out.println("/////////////////////////////////"); 
} 

/* 
* This method checks the logs for entries by the Session ID 
* @param String sessionID - this is the session ID to search for 
* */ 
private static void returnLogLinesBySessionID() { 
    System.out.println("///////By Session ID//////////////"); 
    checkLogs(sessionID); 
    System.out.println("//////////////////////////////////"); 
} 
/* 
* This method checks the logs for entries by the business ID 
* @param String businessID - this is the business ID to search for 
* */ 
private static void returnLogLinesByBusiness() { 
    System.out.println("///////By Business ID/////////////"); 
    checkLogs(businessID); 
    System.out.println("//////////////////////////////////"); 
} 
/* 
* This method checks the logs for entries by the log level 
* @param String logLevel - this is the log level to search for 
* */ 
private static void returnLogLinesByLevel() { 
    System.out.println("///////By Level/////////////"); 
    checkLogs(logLevel); 
    System.out.println("////////////////////////////");  
    } 

/* 
* This method checks if the logs contain a string of characters 
* @param String logContentToSearchFor - The String to search logs for 
* */ 
private static void checkLogs(String logContentToSearchFor){ 
    for(int i = 0; i <= logList.size() - 1; i++) 
    { 
     logString = logList.get(i); 
     if(logString.contains(logContentToSearchFor)){System.out.println(logString);} 
    } 
} 

/* 
* This method checks whether the 'dateToValidate' is inside the specified start/end dates 
* and returns true/false based on the outcome 
* */ 
public static boolean isThisDateWithinRange(String string){ 
    try 
    { 

     Date date = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse(string); 
     Date startDate = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse(startDateStr); 
     Date endDate = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse(endDateStr); 
     if(date.after(startDate) && date.before(endDate)){ 
      return true; 
     } else{ 
      return false; 
     } 
    }catch(ParseException e) {e.printStackTrace(); 
      return false; 
     } 
} 

private static void readFile() { 
    /*Read log file*/ 
    Scanner logScanner = null; 
    try { 
     logScanner = new Scanner(new File("C:\\Users\\"+System.getProperty("user.name")+"\\Desktop\\logs.txt")); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
    while (logScanner.hasNextLine()) { 
     line = logScanner.nextLine(); 
     logList.add(line);     
    } 
} 

}

+0

你是什么意思的范围?你能举一个你期望看到的输出例子吗? –

+0

编号希望看到以下2012-09-13 16:04:22 DEBUG SID:34523 BID:1329 RID:65d33'开始新的会话' 2012-09-13 16:04:30 DEBUG SID:34523 BID:1329 RID:54f22'认证用户' 2012-09-13 16:05:30 DEBUG SID:42111出价:319 RID:65a23'开始新的会话' 2012-09-13 16:04:50错误SID:34523出价: 1329 RID:54ff3'Missing Authentication token' 2012-09-13 16:05:31 DEBUG SID:42111 BID:319 RID:86472'认证用户' 2012-09-13 16:05:31 DEBUG SID:42111 BID :319 RID:7a323'删除ID为543234的资产' 2012-09-13 16:05:32 WARN SID:42111 BID:319 RID:7a323'Inv ID'@MehmetSedatGüngör – CoffeeTime

+1

@CoffeeTime这和你的区别是什么[其他问题](http://stackoverflow.com/questions/23065301/how-to-print-out-a-string-between-certai正日期,从日志文件)? – CodeCamper

回答

0

到澈conditon CK的范围是不正确,应该是:如果日期是小于或的startDate

if(date.after(startDate) && date.before(endDate)){ 
     // valid date 
    } 

你以前的状态将返回true,如果它不是结束日期这是你想要的对面更大。

+0

嗨,它仍然无法正常工作。输出文件的所有内容。 @Kakarot – CoffeeTime

+0

@CoffeeTime从你在问题中提供的数据看起来好像所有的日期都落在这个范围内 – Kakarot

+0

我不能修改数据,所以如何在2012-09-13只过滤日志条目 – CoffeeTime

0

在你的方法isThisDateWithinRange()你是检查是两个日期之间的日期使用变量dateToValidate这从来没有改变(对于每个方法调用它有值“2012-09-13”)。您应该更改方法签名并将想要检查的参数日期传递到范围内。它应该是这样的

public static boolean isThisDateWithinRange(String strDate){ 
    // .... 
    Date date = sdf.parse(strDate); 
    // ... 
} 

随着@Kakarot已经回答了,你应该使用:

if(date.after(startDate) && date.before(endDate)){ 
     // valid date 
    } 

如果使用逻辑OR(||)日期始终是开始日期后或结束日期之前,和在这种情况下,方法isThisDateWithinRange将始终返回true。

0

我将日期作为日期来验证两个日期之间的检查,所以它总是返回true,因此打印出每个日志条目。我需要做的是通过当前日志条目的日期。

if(isThisDateWithinRange(logString.substring(0, 11)))