3

Just asking, and how would you go about doing this.

I know there is ways to get an overall percentage to inform users of the download's progress, but I haven't a clue on how I can do the similar for time.

E.g. "Time until download finishes: 5 minutes".

All I know is percentages, writing the bytes written then dividing it by the length and turning it into a percentage (if I recall correctly, haven't done the above for a few months, so I'm rusty and very forgetful)

Thanks, out of curiosity.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
john
  • 189
  • 1
  • 1
  • 6

2 Answers2

6

For a completely linear model, you simply divide the number of bytes left to download by the so-far-average download speed:

double avgSpeed = (double) bytesDownloaded / timeElapsed;
double timeLeft = byteLeftToDownload / avgSpeed;

If you stick to milliseconds everywhere, timeLeft will contain the estimated number of milliseconds until the full file is downloaded.

To output that properly in terms of hours and/or minutes and/or seconds, I suggest you have a look at

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • Hmm, I don't have enough time and I've been trying to get this working for a bit now: here's my downloader http://pastebin.com/5ajyJxLk – john Sep 05 '11 at 20:50
  • Oh thanks, that works like a charm :), I will convert it to minutes when I get back. – john Sep 05 '11 at 21:15
  • No problem, you're welcome. Make sure you mark the question as solved if your problem has been solved. (That goes for your other questions as well!) – aioobe Sep 05 '11 at 21:24
  • Ok, I'm back. Just did a quick way on the top of my head (only way I can remember), it works and all but how can I format it, so it does not show it's decimals just the first/second digits (e.g. 2 minutes or 20 minutes instead of 20.6328718891 or 0.209029422)... I use: ((timeLeft/60000) % 60) hoping to get the output in minutes; I do not mind if it goes down to 0 minutes, then I could edit the string.. Sorry for the last minute "hopes-up" :/ I'm truly rusty. – john Sep 05 '11 at 21:49
  • Have you heard of a little thing called "integer truncation"? How'bout "formatted printing". Either/both will let you display the number however you wish... – paulsm4 Sep 05 '11 at 22:59
0

Yes, you can.

No, there's nothing "built-in".

And there are actually (at least) two parts to your question:

1) How do I determine the time?

2) How do I display it?

Suggestion: Google for "Java status bar", and look at some code that might do what you're looking for.

Suggestion: Look at MS Vista's "time remaining" algorithm. Whatever Vista is doing - don't do that ;)

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • I know how to display it, but unsure of determining time. – john Sep 05 '11 at 20:53
  • Your initial post didn't specify that :) One of the problems with computing time on-the-fly is the "Vista Problem": you might compute "8 hours left" one sample, and "8 seconds left" the next sample... – paulsm4 Sep 05 '11 at 23:01