Class for program event timing : Timing « Development Class « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Development Class » Timing 




Class for program event timing
   

/*
 * This code is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public 
 * License as published by the Free Software Foundation; either 
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this program; if not, write to the Free 
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, 
 * MA  02111-1307, USA.
 */
//package no.geosoft.cc.util;



import java.util.Date;



/**
 * Class for program event timing.
 * Usage:
 *
 *   <pre>
 *   Timer timer = new Timer();
 *
 *   // do stuff
 *
 *   System.out.println (timer);  // prints time elapsed since
 *                                // object was created.
 *   </pre>
 
 @author <a href="mailto:[email protected]">Jacob Dreyer</a>
 */
public class Timer
{
  private Date  start_;
  

  
  /**
   * Start timer.
   */
  public Timer()
  {
    reset();
  }


  
  /**
   * Returns exact number of milliseconds since timer was started.
   
   @return  Number of milliseconds since timer was started.
   */
  public long getTime()
  {
    Date now = new Date();
    long nMillis = now.getTime() - start_.getTime();

    return nMillis;
  }


  
  /**
   * Restarts the timer.
   */
  public void reset()
  {
    start_ = new Date();  // now    
  }


  
  /**
   * Returns a formatted string showing the elaspsed time
   * suince the instance was created.
   
   @return  Formatted time string.
   */
  public String toString()
  {
    long nMillis = getTime();
    
    long nHours   = nMillis / 1000 60 60;
    nMillis -= nHours * 1000 60 60;
      
    long nMinutes = nMillis / 1000 60;
    nMillis -= nMinutes * 1000  60;

    long nSeconds = nMillis / 1000;
    nMillis -= nSeconds * 1000;
    
    StringBuffer time = new StringBuffer();
    if (nHours > 0time.append (nHours + ":");
    if (nHours > && nMinutes < 10time.append ("0");
    time.append (nMinutes + ":");
    if (nSeconds < 10time.append ("0");
    time.append (nSeconds);
    time.append (".");
    if (nMillis < 100time.append ("0");
    if (nMillis <  10time.append ("0");
    time.append (nMillis);
    
    return time.toString();
  }


  
  /**
   * Testing this class.
   
   @param args  Not used.
   */
  public static void main (String[] args)
  {
    Timer timer = new Timer();

    for (int i = 0; i < 100000000; i++) {
      double b = 998.43678;
      double c = Math.sqrt (b);
    }

    System.out.println (timer);
  }
}
    
    
  














Related examples in the same category
1.Compute and display elapsed time of an operation
2.Get system time using System class
3.Get elapsed time in milliseconds
4.Get elapsed time in seconds
5.Get elapsed time in minutes
6.Get elapsed time in hours
7.Get elapsed time in days
8.Get time in milliseconds using Java Calendar
9.java.util.concurrent.TimeUnit: convert between milliseconds and days
10.Get seconds since
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.