reading in a time that includes milliseconds

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ndedhia1
    New Member
    • Jan 2009
    • 112

    reading in a time that includes milliseconds

    I was reading in a log file like this that had no milliseconds:
    [code=unix]
    QuoteBlockTimin g exceeded 1 ms: 1 --- Thu Dec 10 02:01:40 CST 2009 170.137.15.155 Class key = 601650761 block size
    QuoteBlockTimin g exceeded 1 ms: 1 --- Thu Dec 10 02:01:40 CST 2009 170.137.15.155 Class key = 606887631 block size
    QuoteBlockTimin g exceeded 1 ms: 1 --- Thu Dec 10 02:01:40 CST 2009 170.137.15.155 Class key = 154517966 block size
    QuoteBlockTimin g exceeded 1 ms: 1 --- Thu Dec 10 02:01:40 CST 2009 170.137.15.155 Class key = 69220249 block size
    QuoteBlockTimin g exceeded 1 ms: 1 --- Thu Dec 10 02:01:40 CST 2009 170.137.15.155 Class key = 575941474 block size
    [/code]

    and I was outputting it to an excel file like this:
    [code=excel]
    Class Key Date Start Time End Time Length(ms) Block Size Class BC (TS) Product Class Key Class Type Outlier Host First Tick Open. Rot. Open. (ms) Class Thread #
    606887631 Thu Dec 10 02:01:40 02:01:40 1 1 Unidentified perfgl15 20
    533476053 Thu Dec 10 02:01:40 02:01:40 1 1 Unidentified perfgl15 28
    530369105 Thu Dec 10 02:01:40 02:01:40 1 1 ARB BC20(1) ARB 530369105 3 perfgl15 24
    69208240 Thu Dec 10 02:01:40 02:01:40 1 1 BLSW BC20(2) BLSW 69208240 3 perfgl15 1
    575941474 Thu Dec 10 02:01:40 02:01:40 1 1 CBOU BC20(3) CBOU 575941474 3 perfgl15. 24
    601650761 Thu Dec 10 02:01:40 02:01:40 1 1 DNY BC20(4) DNY 601650761 3 perfgl15. 28
    [/code]

    I was going thru the log file, using spaces as new fields and when i got to the time, i was reading in the time using the ':' as a deliminator and reading in the hrs, min and seconds as ints creating a Calendar variable after putting the individual fields into the GregorianCalend ar constructor like this:

    [code=java]
    if (input.startsWi th("QuoteBlockT iming")) {
    boolean sessionSet = false;
    StringTokenizer ST = new StringTokenizer (input, " \t");
    while (ST.hasMoreToke ns()) {
    String thisToke = ST.nextToken();
    if (thisToke.equal sIgnoreCase("ms :")) {
    returner.setM_d uration(new Integer(ST.next Token()).intVal ue());
    ST.nextToken();
    ST.nextToken();
    String strMonth = ST.nextToken();
    int intMonth = convertMonth(st rMonth);
    Integer intMonthDay = new Integer(ST.next Token());
    String strTime = ST.nextToken();
    StringTokenizer ST2 = new StringTokenizer (strTime, ":");
    Integer intHour = new Integer(ST2.nex tToken());
    Integer intMinute = new Integer(ST2.nex tToken());
    Integer intSecond = new Integer(ST2.nex tToken());

    ST.nextToken();
    Integer intYear = new Integer(ST.next Token());

    Calendar cal = new GregorianCalend ar(intYear.intV alue(),intMonth ,
    intMonthDay.int Value(),intHour .intValue(),int Minute.intValue (),
    intSecond.intVa lue());


    returner.setM_d ate(cal.getTime ());
    [/code]

    NOW, with milliseconds in the log file that looks like this:
    [code=unix]
    QuoteBlockTimin g exceeded 1 ms: 1 --- Thu Dec 10 02:01:40.364 CST 2009 170.137.15.155 Class key = 601650761 block size
    QuoteBlockTimin g exceeded 1 ms: 1 --- Thu Dec 10 02:01:40.364 CST 2009 170.137.15.155 Class key = 606887631 block size
    QuoteBlockTimin g exceeded 1 ms: 1 --- Thu Dec 10 02:01:40.364 CST 2009 170.137.15.155 Class key = 154517966 block size
    QuoteBlockTimin g exceeded 1 ms: 1 --- Thu Dec 10 02:01:40.366 CST 2009 170.137.15.155 Class key = 69220249 block size
    QuoteBlockTimin g exceeded 1 ms: 1 --- Thu Dec 10 02:01:40.366 CST 2009 170.137.15.155 Class key = 575941474 block size
    QuoteBlockTimin g exceeded 1 ms: 1 --- Thu Dec 10 02:01:40.367 CST 2009 170.137.15.155 Class key = 154517966 block size
    [/code]

    I need to put the milliseconds in the excel file also.

    I was thinking about changing the line:
    Integer intSecond = new Integer(ST2.nex tToken()); INTO
    Double intSecond = new Double(ST2.nextToken( ));

    but the GregorianCalend ar doesnt have a constructor that has a double argument that it accepts.

    I was also thinking about using SimpleDateForma t but i was unsure of how to use that.

    I really only need the hours:minutes:s econds.millisec onds in any kind of format

    Please help.

    Thanks
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    You are separating the time-string at ":", Before you had for example seconds="40". With the new data you have seconds="40.364 ".
    So you cannot convert that to an integer anymore like before! Just separate this string at "." and assign the first part to seconds, the second part to milliseconds!

    Code:
    String strSeconds = ST2.nextToken();
    StringTokenizer ST3 = new StringTokenizer(strSeconds , "."); 
    Integer intSecond = new Integer(ST3.nextToken()); 
    Integer intMilliseconds = new Integer(ST3.nextToken());
    By the way, StringTokenizer is old stuff! Just use Regular Expressions!
    With regular expressions, you could do all your splitting at once and less code.
    Look here:

    Code:
        import java.util.regex.*
    
        CharSequence inputStr = "QuoteBlockTiming exceeded 1 ms: 1 --- Thu Dec 10 02:01:40.364 CST 2009 170.137.15.155 Class key = 601650761 block ...";
        String patternStr = " --- (...) (...) (..) (..):(..):(..)\\.(...) CST (....) ";
        
        Matcher matcher = Pattern.compile(patternStr).matcher(inputStr);    
        if (matcher.find()) {
           String all =         matcher.group(0); // whole String " --- Thu Dec 10 02:01:40.364 CST 2009 "
           String dayInWeek =   matcher.group(1); // returns "Thu"
           String month =       matcher.group(2); // returns "Dec" 
           String dayInMonth =  matcher.group(3); // returns "10"
           String hour =        matcher.group(4); // returns "02"
           String minute =      matcher.group(5); // returns "01"
           String second =      matcher.group(6); // returns "40"
           String millisecond = matcher.group(7); // returns "364"
           String year=         matcher.group(8); // returns "2009"
        }

    Comment

    Working...