Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Timing and formatting the time between calls.  RSS feed

 
Brian Tkatch
Bartender
Posts: 598
26
Linux Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Android Log feature puts down a timestamp of MM-DD HH:MM:SS.FF3 (FF3 means 3 digits after the decimal). I would like to automatically calculate the time difference between two log entries and output it at the begining of the log, with a set amount of digits, so it is easy to read.

I do not have access to the timestamp (that i know of), so, grabbing the time myself seems like the option. I have been trying System.nanoTime, but subtracting two of them gives me different amounts of digits, besides being far more granular than i would like. Even System.currentTimeMillis() has too much information, but i guess that is okay if i cam just going to format it afterward. Formatting seems to have plenty of options too, with String.format, NumberFormat, and others.

All i want is to calculate the difference in seconds with 3 sub-second digits, with the average time being less than a second, but going over 1 second might happen rarely. This is just for a test (in the Android forum) but i want to learn an appropriate approach. I keep thinking i know it, and then when searching google, i end up back here.
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The appropriate approach would be using java.time, but I don't think Android supports Java 8 yet.

You should create a DateFormat, then parse the log lines to Date objects, which you can then convert to millisecond timestamps:
 
Brian Tkatch
Bartender
Posts: 598
26
Linux Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does not look like it supports java.time. When i typed it in, autocode did not suggest anything.

I do not think i have access to the log line itself. So, the timestamp or whatever would have to be generated in the code.
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, I didn't understand you, I thought you wanted to convert the log lines.
Brian Tkatch wrote:I would like to automatically calculate the time difference between two log entries


I suppose you could take System.currentTimeMillis() twice, subtract them from each other, divide the result by 1000d, and then format the result to use 3 decimals.
 
Brian Tkatch
Bartender
Posts: 598
26
Linux Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Ah, I didn't understand you, I thought you wanted to convert the log lines.

I did. But i don't think i have access to them Basically, i want the equivalent, because that works for me.

Stephan van Hulst wrote:I suppose you could take System.currentTimeMillis() twice, subtract them from each other

Got that. Did italready.

Stephan van Hulst wrote:divide the result by 1000d, and then format the result to use 3 decimals.

Please help me with that last part.
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at String.format(). How to use format strings is described in java.util.Formatter. The 'f' conversion applies to your case.
 
Brian Tkatch
Bartender
Posts: 598
26
Linux Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Take a look at String.format(). How to use format strings is described in java.util.Formatter. The 'f' conversion applies to your case.

Too confusing. So, i went back to DecimalFormat():
The numbers are slightly different than the log's time, but at this granularity, the calls themselves probably take up some time.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Tkatch wrote:Too confusing. So, i went back to DecimalFormat():

May I make a comment? You're on Android, which is an OS generally used for mobile phones. Why would you want to be checking log files with a mobile phone?

I can certainly understand wanting to send them to a server that can return all sorts of "mining" information about them - including a duration, if need be; but parsing large files in real time on the device and then converting Strings to do it, wouldn't be my first "solution".

But I'm certainly no expert in these things, so maybe it's "standard" these days...

Winston
 
Brian Tkatch
Bartender
Posts: 598
26
Linux Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:May I make a comment? You're on Android, which is an OS generally used for mobile phones. Why would you want to be checking log files with a mobile phone?

I guess i wasn't clear. The log is shown by Android Studio when running an app. If you look in the Android forum, you will see that i am researching an issue where things are going awry. The particular thing i am working on right now (and hope to post today or Tuesday) is getting particular about timing, as events seem to fire either out of order or not at all.

Anyway, i have been manually subtracting the log times in my head, and although that sufficed until now, it's getting tedious, leading me to want to do it automatically. I don't think i have access to the actual log lines, so, i need to add a time element myself.
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Tkatch wrote:Too confusing.

I strongly suggest reading about format strings anyway. They are crazy useful, and once you get the hang of them you can print out pretty much anything exactly the way you want.

In this case, you'll probably want to do something like the following:
 
Brian Tkatch
Bartender
Posts: 598
26
Linux Notepad Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:I strongly suggest reading about format strings anyway. They are crazy useful, and once you get the hang of them you can print out pretty much anything exactly the way you want.

In this case, you'll probably want to do something like the following:

Tanx. DecimalFormat() works for me in this case, and i know of formatting, just not too much and almost no practice. So, it's going to have to wait for another time.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!