Measuring Time in Seconds using Java

View previous topic View next topic Go down

Java Measuring Time in Seconds using Java

Post by BIT0122-Amit on Thu Sep 29, 2011 3:46 am

Today tanvir from 3rd batch was asking me how he can measure time in Java using seconds.

Well, here it is!
Code:

package time;

import java.util.concurrent.TimeUnit;

public class Timer

   private long endTime;
   private long startTime;
   private int delayInThread;

   private void StartTime() {
      System.out.println("Starting counter.");
      startTime = System.currentTimeMillis();
   }

   private void EndTime() {

      endTime = System.currentTimeMillis();

      System.out.println("Stopped Counter");
   }

   private long MeasureDelayInSecond() {
      return TimeUnit.MILLISECONDS.toSeconds(endTime - startTime);
   }

   public void MesaureTime(int delayInSecond) {

      delayInThread = delayInSecond * 1000;

      StartTime();
      
      System.out.println("Starting Thread to pause program for "
            + delayInSecond + " second(s)");
      try {
         Thread.sleep(delayInThread);
      } catch (InterruptedException e) {
         e.printStackTrace();
      }

      EndTime();
      System.out.println("The delay recorded is: " + MeasureDelayInSecond());

   }
}

Code:

import time.Timer;

public class Main {

   public static void main(String[] args) {

      Timer t = new Timer();
      t.MesaureTime(5);
   }

}

It pauses the whole program by using thread.sleep method. Thread.Sleep accepts measurements in Seconds*1000 in case you don't know.

MeasureDelayInSeconds is used to show that the counter calculated the time exactly equal to the amount of time the thread was sleeping for.


Last edited by BIT0122-Amit on Thu Sep 29, 2011 4:00 am; edited 1 time in total

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Java Re: Measuring Time in Seconds using Java

Post by BIT0305-tanvir on Thu Sep 29, 2011 3:59 am

Ahahahahahahahhahahahahha .... eta ki Dekhlum ami....thank you amit vai..

BIT0305-tanvir
Pre-Alpha Release
Pre-Alpha Release

Course(s) :
  • N/A

Blood Group : NULL
Posts : 7
Points : 10

View user profile

Back to top Go down

Java Re: Measuring Time in Seconds using Java

Post by BIT0122-Amit on Thu Sep 29, 2011 4:01 am

:p Code দেখলে!

ভালো কথা, উপরের দুই লাইন মানে এগুলা-
Code:
package time;

import java.util.concurrent.TimeUnit;

কোড ট্যাগে ছিল না। সেটা ঢুকিয়ে দিয়েছি মাত্র।

আরো কোন সাহায্য লাগলে বলো। আমি আছি Smile

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Java Re: Measuring Time in Seconds using Java

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum