1

I read through previous posting and learned the following, but it still isn't working. Can anyone help me?

long startTime = System.currentTimeMillis();
......
long endTime = System.currentTimeMillis();
long different  = endTime - startTime;

System.out.println("different : " + different);

long s = (different / 1000) % 60;
long m = (different / (1000 * 60)) % 60;
long h = (different / (1000 * 60 * 60)) % 24;
System.out.println(String.format("%d:%02d:%02d", h,m,s));  

Why is my output time still 0:00:00?

[output]
different : 473
0:00:00
Simon Verhoeven
  • 1,295
  • 11
  • 23
BeyondProgrammer
  • 893
  • 2
  • 15
  • 32

4 Answers4

3

Because 473 ms < 1s, so you get 0:00:00.

Blub
  • 3,762
  • 1
  • 13
  • 24
1

you are running for 473 milliseconds. that's half a second.

You are then displaying the hours minutes and seconds of execution, which is 0 for all of them.

Your output is correct.

M21B8
  • 1,867
  • 10
  • 20
0

The output which you're getting now is logically correct one. Also display milliseconds in your print statement if you want calculate milliseconds with your time.

System.out.println(String.format("%02d:%02d:%02d.%03d", h,m,s,mls)); 
Vignesh Vino
  • 1,242
  • 4
  • 25
  • 50
0

tl;dr

Instant now = Instant.now();
String output = Duration.between( now , 
                                  now.plus( 473 , ChronoUnit.MILLISECONDS ) )
                        .toString();

PT0.473S

Details

As others stated, 473 milliseconds is less than a second so it appears as zero whole seconds.

ISO 8601

Do not track elapsed time using time-of-day formatting. It is ambiguous and confusing. And suffers from this kind of fractional problem depending on your format details.

Instead, use the standard ISO 8601 format for a span of time: PnYnMnDTnHnMnS where P marks the beginning and T separates the years-months-days from the hours-minutes-seconds.

You are using troublesome old date-time classes now supplanted by the java.time classes.

Instant

The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds.

Instant instant = Instant.now(); 

Let's simulate input data by getting a moment a few minutes in the future.

Instant future = instant.plus( 473 , ChronoUnit.MILLISECONDS );

Duration

The Duration class represents a span of time unattached from the timeline.

Duration duration = Duration.between( instant , future );

Generate a String in ISO 8601 format.

String output = duration.toString();  

PT0.473S

Internally tracked as a number of whole seconds plus a fractional second as a number of nanoseconds. So think of it as:

Duration = ( Seconds + Nanos )

You can ask for the total number of whole seconds and the fractional second as nanoseconds.

long seconds = duration.getSeconds();
int nanos = duration.getNano();

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to java.time.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154