Thursday 15 August 2013

java - Date calculation - progressbar showing timeleft -


I have to show a simple progress bar, so that I can be shown how often some process takes. At the time of its creation, I only have the actual percentage (in the form of the integer) and the time is left (as string formatted HH: mm: SS). I want to update it every second and show me the actual status process. I have tried everything and it does not work. The current version looks like this - please let me know what I'm doing ...

  int initialprogress = 35; //% // Timeout expiration date to end process = New SimpleDateFormat ("HH: mm: SS"). Pars ("07:07:07"); Date now = new date (); Longest total duration = (long) (((double (endDate.getTimeInMillis) - now .getTimeInMillis ()) * 100.0 / (double) initial progress)   

and then every Second I repeat:

  now = new date (); int current progress = (total duration - endDate.getTimeInMillis () + now .getTimeInMillis ()) / total duration;   

It's not just working. The total duration is also something weird ...

The issue is that you have a time remaining string and you want to parse it for the percentage of work

The first thing is needed, obviously, this is the expected time required. There is also a string .

first its hh Write a method to parse: ss string to long> representing the remaining time in seconds.

 < Code> Public long parasetaconds (throws the last string) throws ParseException {Last Message Format Term Fermat = New MessageFor mat ("{0, number, #}: {1, number, #}: {2, number, #}"); Last item [] Time parsed = Period format. Duration (duration); Last Long Total Duration = Period Ytti.H.Hors.Second (Parsightime Timing [0]) + Time Unit. MINUTSSESSES (Long PARTT Time Remanning [1]) + (Long) Parset Time Remaining [2]; Total return; }   

What we do here is MessageFormat to parse an string in an array of objects Use . As we have told MessageFormat that these numbers are, it will automatically convert exception (either attempts to convert) to long on .

Once we have those numbers that we scale them to the second by using (very useful) TimeUnit class.

Some quick tests to make sure we are on the right track:

  system.out.println (parseToSeconds ("00:00:01")); Println (parseToSeconds ("00:01:00")); Println (parseToSeconds ("01:00:00")); Println (parseToSeconds ("01:01:01"));   

Output:

1
60
3600
3661

Looks good

We feel that as the beginning of the process is correct that we get the remaining time for the simplicity of "04:04:04", it gives 14644 . Now we need to calculate the percentage against storing it and any new duration string This should do the trick:

  percent in the form of public int (last Long total time, last long balance) {last double percentage = balance / ((double total) time); Return (percentage * 100); }   

Please note the fact that I put one of the items (roughly redundant) in the double . The reason for this is that any operation on an integral type in Java always gives another integral type. For a double to force it to return double

make a quick check again:

  last long pigment = 14644; System.out.println (as percentages (total duration, parseToSeconds ("03:03:03"))); System.out.println (as per percentage (total duration, parseToSeconds ("02:02:02"))); System.out.println (as per percentage (total duration, parseToSeconds ("01:01:01")));   

Output:

75
50
25

Good, it's time In the form of a balance, the remaining one probably reverses what we want for a progress bar:

  percent in the form of public fixed int (last long time, last long balance) {Last double percentage = balance / (total time) (double); Return 100 - (integer) (percentage * 100); }   

Output:

25
50
75

Ah-ha very good.

No comments:

Post a Comment