Measure time elapsed in Java

0 votes
public class Stream
{
    public startTime;
    public endTime;
    public getDuration()
    {
        return startTime - endTime;
    }
}

It is also important that for example if the startTime it's 22:00 and endTime 1:00 . I want to get a duration of 3:00.

How can I accomplish this in Java?

Jul 4, 2018 in Java by samarth295
• 2,220 points
866 views

1 answer to this question.

0 votes

This is also a way to work with timings:

public class Ets {
    public Ets() {
    reset();
    }
    public void reset() {
    t0=System.nanoTime();
    }
    public long t0() {
        return t0;
    }
    public long dt() {
        return System.nanoTime()-t0();
    }
    public double etms() {
    return etms(dt());
    }
    @Override public String toString() {
        return etms()+" ms.";
    }
    public static double etms(long dt) {
        return dt/1000000.; // 1_000_000. breaks cobertura
    }
    private Long t0;
}
answered Jul 4, 2018 by scarlett
• 1,290 points

Related Questions In Java

0 votes
2 answers

How can I get current time in YYYY:MM:DD HH:MI:Sec:Millisecond format in Java?

public String getCurrentTimeStamp() { ...READ MORE

answered Sep 21, 2018 in Java by Parth
• 4,640 points
7,577 views
0 votes
1 answer

How to calculate method execution time in Java ?

Use the following code : new Timer(""){{ ...READ MORE

answered May 25, 2018 in Java by Rishabh
• 3,620 points
1,706 views
0 votes
1 answer

What is Just in time compiler in Java?

The Just-In-Time compiler is one of the integral parts ...READ MORE

answered Jul 14, 2019 in Java by Frankie
• 9,830 points
749 views
0 votes
0 answers

How to manage two JRadioButtons in java so that only one of them can be selected at a time?

How to manage two JRadioButtons in java ...READ MORE

Apr 21, 2020 in Java by kartik
• 37,520 points
721 views
0 votes
1 answer

How can I get the current date and time in UTC or GMT in Java?

This definitely returns UTC time: as String ...READ MORE

answered Jun 7, 2018 in Java by Rishabh
• 3,620 points
27,654 views
+1 vote
2 answers

Calculating date/time difference in Java

We can also use java.util.concurrent.TimeUnit class. long diff = d2.getTime() ...READ MORE

answered Aug 28, 2018 in Java by Sushmita
• 6,920 points
25,884 views
0 votes
1 answer

How to get the current date/time in Java?

It depends on what form of date/time ...READ MORE

answered Dec 30, 2020 in Java by Gitika
• 65,770 points
1,290 views
0 votes
3 answers

Increment the date in Java by 1-day

import java.time.LocalDate; public class DateIncrementer { static ...READ MORE

answered Aug 1, 2018 in Java by Akrati
• 3,190 points
4,558 views
0 votes
3 answers

Is there any goto statement in java?

So they could be used one day ...READ MORE

answered Oct 15, 2018 in Java by Daisy
• 8,140 points
1,331 views
0 votes
1 answer

what do you mean by static block in java?

Static block is used for initializing the ...READ MORE

answered Jun 18, 2018 in Java by scarlett
• 1,290 points
924 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP