By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,247 Members | 871 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,247 IT Pros & Developers. It's quick & easy.

reading in a time that includes milliseconds

100+
P: 112
I was reading in a log file like this that had no milliseconds:
Expand|Select|Wrap|Line Numbers
  1. QuoteBlockTiming exceeded 1 ms: 1 --- Thu Dec 10 02:01:40 CST 2009 170.137.15.155 Class key = 601650761 block size
  2. QuoteBlockTiming exceeded 1 ms: 1 --- Thu Dec 10 02:01:40 CST 2009 170.137.15.155 Class key = 606887631 block size
  3. QuoteBlockTiming exceeded 1 ms: 1 --- Thu Dec 10 02:01:40 CST 2009 170.137.15.155 Class key = 154517966 block size
  4. QuoteBlockTiming exceeded 1 ms: 1 --- Thu Dec 10 02:01:40 CST 2009 170.137.15.155 Class key = 69220249 block size
  5. QuoteBlockTiming exceeded 1 ms: 1 --- Thu Dec 10 02:01:40 CST 2009 170.137.15.155 Class key = 575941474 block size
  6.  
and I was outputting it to an excel file like this:
Expand|Select|Wrap|Line Numbers
  1. 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 #
  2. 606887631    Thu Dec 10    02:01:40    02:01:40    1    1        Unidentified                perfgl15                20
  3. 533476053    Thu Dec 10    02:01:40    02:01:40    1    1        Unidentified                perfgl15                28
  4. 530369105    Thu Dec 10    02:01:40    02:01:40    1    1    ARB    BC20(1)    ARB    530369105    3    perfgl15                24
  5. 69208240    Thu Dec 10    02:01:40    02:01:40    1    1    BLSW    BC20(2)    BLSW    69208240    3    perfgl15                1
  6. 575941474    Thu Dec 10    02:01:40    02:01:40    1    1    CBOU    BC20(3)    CBOU    575941474    3    perfgl15.                24
  7. 601650761    Thu Dec 10    02:01:40    02:01:40    1    1    DNY    BC20(4)    DNY    601650761    3    perfgl15.            28
  8.  
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 GregorianCalendar constructor like this:

Expand|Select|Wrap|Line Numbers
  1. if (input.startsWith("QuoteBlockTiming")) {
  2.             boolean sessionSet = false;
  3.             StringTokenizer ST = new StringTokenizer(input, " \t");
  4.             while (ST.hasMoreTokens()) {
  5.                 String thisToke = ST.nextToken();
  6.                 if (thisToke.equalsIgnoreCase("ms:")) {
  7.                     returner.setM_duration(new Integer(ST.nextToken()).intValue());
  8.                     ST.nextToken();
  9.                     ST.nextToken();
  10.                     String strMonth = ST.nextToken();
  11.                     int intMonth = convertMonth(strMonth);
  12.                     Integer intMonthDay = new Integer(ST.nextToken());
  13.                     String strTime = ST.nextToken();
  14.                     StringTokenizer ST2 = new StringTokenizer(strTime, ":");
  15.                     Integer intHour = new Integer(ST2.nextToken());
  16.                     Integer intMinute = new Integer(ST2.nextToken());
  17.                     Integer intSecond = new Integer(ST2.nextToken());
  18.                     ST.nextToken();
  19.                     Integer intYear = new Integer(ST.nextToken());
  20.  
  21.                     Calendar cal = new GregorianCalendar(intYear.intValue(),intMonth,
  22.                           intMonthDay.intValue(),intHour.intValue(),intMinute.intValue(),
  23.                         intSecond.intValue());
  24.  
  25.                    returner.setM_date(cal.getTime());
  26.  
NOW, with milliseconds in the log file that looks like this:
Expand|Select|Wrap|Line Numbers
  1. QuoteBlockTiming exceeded 1 ms: 1 --- Thu Dec 10 02:01:40.364 CST 2009 170.137.15.155 Class key = 601650761 block size
  2. QuoteBlockTiming exceeded 1 ms: 1 --- Thu Dec 10 02:01:40.364 CST 2009 170.137.15.155 Class key = 606887631 block size
  3. QuoteBlockTiming exceeded 1 ms: 1 --- Thu Dec 10 02:01:40.364 CST 2009 170.137.15.155 Class key = 154517966 block size
  4. QuoteBlockTiming exceeded 1 ms: 1 --- Thu Dec 10 02:01:40.366 CST 2009 170.137.15.155 Class key = 69220249 block size
  5. QuoteBlockTiming exceeded 1 ms: 1 --- Thu Dec 10 02:01:40.366 CST 2009 170.137.15.155 Class key = 575941474 block size
  6. QuoteBlockTiming exceeded 1 ms: 1 --- Thu Dec 10 02:01:40.367 CST 2009 170.137.15.155 Class key = 154517966 block size
  7.  
I need to put the milliseconds in the excel file also.

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

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

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

I really only need the hours:minutes:seconds.milliseconds in any kind of format

Please help.

Thanks
Dec 10 '09 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 785
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!

Expand|Select|Wrap|Line Numbers
  1. String strSeconds = ST2.nextToken();
  2. StringTokenizer ST3 = new StringTokenizer(strSeconds , "."); 
  3. Integer intSecond = new Integer(ST3.nextToken()); 
  4. Integer intMilliseconds = new Integer(ST3.nextToken());
  5.  
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:

Expand|Select|Wrap|Line Numbers
  1.     import java.util.regex.*
  2.  
  3.     CharSequence inputStr = "QuoteBlockTiming exceeded 1 ms: 1 --- Thu Dec 10 02:01:40.364 CST 2009 170.137.15.155 Class key = 601650761 block ...";
  4.     String patternStr = " --- (...) (...) (..) (..):(..):(..)\\.(...) CST (....) ";
  5.  
  6.     Matcher matcher = Pattern.compile(patternStr).matcher(inputStr);    
  7.     if (matcher.find()) {
  8.        String all =         matcher.group(0); // whole String " --- Thu Dec 10 02:01:40.364 CST 2009 "
  9.        String dayInWeek =   matcher.group(1); // returns "Thu"
  10.        String month =       matcher.group(2); // returns "Dec" 
  11.        String dayInMonth =  matcher.group(3); // returns "10"
  12.        String hour =        matcher.group(4); // returns "02"
  13.        String minute =      matcher.group(5); // returns "01"
  14.        String second =      matcher.group(6); // returns "40"
  15.        String millisecond = matcher.group(7); // returns "364"
  16.        String year=         matcher.group(8); // returns "2009"
  17.     }
  18.  
Dec 15 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.