2014-04-14 211 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)))