开发者

Want java code for calculating time between various logs

开发者 https://www.devze.com 2023-03-14 11:58 出处:网络
[Tue Jun 21 16:09:27.530 2011] request_suspend_state: sleep (0->3) at 263958221465 (2000-01-01 00:04:22.124816866 UTC)
[Tue Jun 21 16:09:27.530 2011] request_suspend_state: sleep (0->3) at 263958221465 (2000-01-01 00:04:22.124816866 UTC)
[Tue Jun 21 16:09:27.530 2011] WARNING omaplfb (OMAPLFBPresentSync 235): Unable to sync with display 1!
[Tue Jun 21 16:09:27.530 2011] stop_drawing_early_suspend: timeout waiting for userspace to stop drawing
[Tue Jun 21 16:09:27.530 2011] WARNING omaplfb (OMAPLFBPresentSync 235): Unable to sync with display 1!
[Tue Jun 21 16:09:27.530 2011] PM: Syncing filesystems ... done.
[Tue Jun 21 16:09:27.545 2011] PM: Preparing system for mem sleep
[Tue Jun 21 16:09:27.545 2011] Freezing user space process开发者_运维知识库es ... (elapsed 0.02 seconds) done.

This is a log file which contains day,time. I want to calculate time between any two lines. Kindly help me in this.


  1. Read each entry, along with their time stamp.
  2. Parse the timestamp String into a java.util.Date.
  3. Use java.util.Calendar or JODA to calculate the time difference.


I would write a routine that takes a list of Strings, parses the dates out of them, and returns a resulting list of dates:

public static List<Date> parseDates(List<String> ss) throws ParseException {
  DateFormat format = new SimpleDateFormat("[EEE MMM dd HH:mm:ss.SSS yyyy]");
  List<Date> dates = new ArrayList<Date>();
  for (int i=0; i<ss.size(); i++) {
    dates.add(format.parse(ss.get(i)));
  }
  return dates;
}

Then you can calculate the number of milliseconds between any two numbered lines like so:

List<Date> dates = parseDates(myLogLines);
long t0 = dates.get(0).getTime(); // Time in millis from line 0.
long t1 = dates.get(1).getTime(); // Time in millis from line 1.
long diff = (t1 - t0); // Elapsed time in milliseconds between lines 0 and 1.


You can extract the two times as a String, use SimpleDateFormat to turn them into a Date and then a long and compare the differences.

Are you using a Windows system? Your accuracy might be about 15 ms.


You gonna need to parse your log and save the tokens in a List or a Map (you could use a StringTokenizer). Then you just need to do some operation on dates using SimpleDateFormat

0

精彩评论

暂无评论...
验证码 换一张
取 消