StopWatch reports elapsed time between start and stop : StopWatch « 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 » StopWatch 




StopWatch reports elapsed time between start and stop
  
/*
 * Copyright 2003-2004 Michael Franken, Zilverline.
 *
 * The contents of this file, or the files included with this file, are subject to
 * the current version of ZILVERLINE Collaborative Source License for the
 * Zilverline Search Engine (the "License"); You may not use this file except in
 * compliance with the License.
 *
 * You may obtain a copy of the License at
 *
 *     http://www.zilverline.org.
 *
 * See the License for the rights, obligations and
 * limitations governing use of the contents of the file.
 *
 * The Original and Upgraded Code is the Zilverline Search Engine. The developer of
 * the Original and Upgraded Code is Michael Franken. Michael Franken owns the
 * copyrights in the portions it created. All Rights Reserved.
 *
 */


/**
 * StopWatch reports elapsed time between start and stop.
 
 @author Michael Franken
 @version $Revision: 1.12 $
 */
public class StopWatch {
    /** Time the Stopwatch started. */
    private long start = 0;

    /** Time the Stopwatch stopped. */
    private long stop = 0;

    /**
     * Start ticking, resets the watch.
     */
    public final void start() {
        start = System.currentTimeMillis();
    }

    /**
     * Stop ticking.
     */
    public final void stop() {
        stop = System.currentTimeMillis();
    }

    /**
     * Calculates time elapsed. If stop has not been called yet, the current time is taken to calculate elapsed time.
     
     @return the time elapsed between start and stop as String. The string contains two of days, hours, minutes, seconds and
     *         milliseconds.
     
     @throws StopWatchException If StopWatch was never started.
     */
    public final String elapsedTime()  {
        long difference;

        if (stop == 0) {
            long now = System.currentTimeMillis();

            difference = (now - start)// in millis
        else {
            difference = (stop - start)// in millis
        }

   

        long mils = difference % 1000;

        difference = (difference - mils1000// in seconds

        long secs = difference % 60;

        difference = (difference - secs60// in minutes

        long minutes = difference % 60;

        difference = (difference - minutes60// in hours

        long hours = difference % 24;

        difference = (difference - hours24// in days

        long days = difference;
        String message = "";

        if (days > 0) {
            message = days + " days and  " + hours + " hours";
        else if (hours > 0) {
            message = hours + " hours and " + minutes + " minutes";
        else if (minutes > 0) {
            message = minutes + " minutes and " + secs + " seconds";
        else if (secs > 0) {
            message = secs + " seconds and " + mils + " milliseconds";
        else {
            message = mils + " milliseconds";
        }

        return message;
    }
}

   
    
  














Related examples in the same category
1.StopWatch provides a convenient API for timings.
2.Simulates a stop watch with a lap counter.
3.Provides the programatic analog of a physical stop watch
4.Basic Timer
5.Provides various features related to the current date and timeProvides various features related to the current date and time
6.Simple stop watch
7.Some simple stop watch.
8.Stop Watch with PrintWriter
9.Stop Watch from JGraphT
10.Stop watch with nano second
11.Stopwatch is a debugging tool for manually profiling code execution
12.Your own timer
13.A class for timing things.
14.A convenience class to do code profiling.
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.