2

I have a txt file with a line like below:

0    Apr 12 08:42:44.000009 (+0.000009) *** START ***

The information I want to get is:

Apr 12 08:42:44

The current method I'm using use is using a scanner to read this line:

public void getTime() throws IOException {
    String time = "";
    Scanner scan = new Scanner(location);
    String firstLine = scan.nextLine();
    String[] splitString = firstLine.split("\\.");
    String[] rebootTime = splitString[0].split(" ");

    for(int i = 0; i < rebootTime.length; i++) {
        if(i != 0) {
            time = time + rebootTime[i] + " ";
        }
    }
    System.out.println(time);
}

Is there a smarter way to get the time information?

After I get the time, how do I transfer it to a date format then calculate the duration?

I'm trying to use JAVA 8 Instant with this method, how can I transfer the time value to a Instant type?

Bas
  • 4,423
  • 8
  • 36
  • 53
Minwu Yu
  • 311
  • 1
  • 6
  • 24
  • 1
    This entirely depends on the nature of your log file. Unless you show the major portion of log file, the answers, if given, would be futile based upon 1 line of parsing! – Am_I_Helpful Aug 02 '17 at 17:45
  • @Am_I_Helpful, since I only need the start time of this log file, so I'm assuming one line parsing should be enough in my case. Am I correct? – Minwu Yu Aug 02 '17 at 17:56

4 Answers4

3

If I understand you properly your goal is to extract reboot time from each string of a log file. Using Scanner is ok to my mind. After extracting a line from log file you might as well use regular expressions on it, like this:

String firstLine = scan.nextLine();
Pattern pattern = Pattern.compile("[a-zA-Z]{3}\\s\\d{2}\\s\\d{2}:\\d{2}:\\d{2}");
Matcher matcher = pattern.matcher(firstLine);

if (matcher.find()) {
    String rebootTime = matcher.group();
}

This regexp is not perfect but it works on your line. You can make it more or less strict.

As to formatting the string to a LocalDateTime, you can use following method:

DateTimeFormatter formatter = new DateTimeFormatterBuilder()
            .appendPattern("MMM dd HH:mm:ss")
            .parseDefaulting(ChronoField.YEAR, 1)
            .toFormatter();

LocalDateTime date = LocalDateTime.parse(rebootTime, formatter);

So parseDefaulting(ChronoField.YEAR, 1) means that you ignore year in string and set it as 1 in resulting LocalDateTime. After that you can calculate durations using LocalDateTimes.

Mongwo
  • 46
  • 2
  • that works, tkx. Do you any idea how can I transfer the rebootTime to a date format so that I can calculate the duration using JAVA Duration class? – Minwu Yu Aug 02 '17 at 18:17
1

I like Mongwo's elegant solution.

There are many ways to skin this cat. Other than regular expression, you can simply use a quick-n-dirty one liner, if it is in fixed length and always starting from the fixed index of a string:

    String rawStr = "0    Apr 12 08:42:44.000009 (+0.000009) *** START ***";        
    System.out.println(rawStr.substring(5, 20));
Pete T
  • 456
  • 2
  • 8
0

If the file is small enough that you are ok with reading the whole thing, it is generated by another process so that you can guarantee the format, and the line you want is first (which from the question I think all above things should be true), your solution can be as simple as

private SimpleDateFormat format = new SimpleDateFormat("MMM dd HH:mm:ss");

public Date read(String filePath) throws URISyntaxException, IOException, ParseException {
    Path fileLocation = Paths.get(filePath);
    byte[] data = Files.readAllBytes(fileLocation);
    return format.parse(new String(data, 5, 15));
}

If the file is longer, you may want to use a scanner if you dont want to read the whole thing, but imho it is still simplest to use indices to get the part of the string that you want.

If you want a very elegant solution maybe you could use a regex, but I really dont think there is much of a need.

aiguy
  • 671
  • 5
  • 20
0

My try here is:-

First let's extract value before dot(".") and then value after double space(" ").

public static void main(String[] args) {
    String str = "0    Apr 12 08:42:44.000009 (+0.000009) *** START ***";
    String[] str1 = str.split("\\.");

    String[] str2 = str1[0].split("\\s{2,}");
    System.out.println((str2[1]));

  }

To understand \\s{2,} you can look into saved regex. regex to match 2 spaces

sumitya
  • 2,631
  • 1
  • 19
  • 32