Provides various features related to the current date and time : 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 




Provides various features related to the current date and time
Provides various features related to the current date and time
  
//----------------------------------------------------------------------------//
//                                                                            //
//                                 C l o c k                                  //
//                                                                            //
//  Copyright (C) Herve Bitteur 2000-2009. All rights reserved.               //
//  This software is released under the GNU General Public License.           //
//  Please contact [email protected] to report bugs & suggestions. //
//----------------------------------------------------------------------------//
//

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Class <code>Clock</code> provides various features related to the current
 * date and time, as well as the elapsed time since the beginning of the
 * application.
 *
 @author Herv&eacute; Bitteur
 @version $Id: Clock.java,v 1.8 2009/03/03 19:45:52 hbitteur Exp $
 */
public class Clock
{
    //~ Static fields/initializers ---------------------------------------------

    /** To have a reference time */
    private static long startTime = System.currentTimeMillis();

    /** General date formatting */
    private static DateFormat dateFormatter = DateFormat.getDateTimeInstance(
        DateFormat.FULL,
        DateFormat.FULL,
        Locale.US);

    /** General time formatting. Locale to be used, could be:  //Locale.US;
       //Locale.FRANCE; */
    private static Locale locale = Locale.getDefault();

    /** Corresponding number format */
    private static NumberFormat nf = NumberFormat.getNumberInstance(locale);

    /** Decimal format */
    private static DecimalFormat timeFormatter = (DecimalFormatnf;

    static {
        timeFormatter.applyPattern("000,000.00");
    }

    //~ Constructors -----------------------------------------------------------

    //-------//
    // Clock // To prevent instantiation
    //-------//
    private Clock ()
    {
    }

    //~ Methods ----------------------------------------------------------------

    //---------//
    // getDate //
    //---------//
    /**
     * Retrieves the values of current date and time of day, and formats a
     * standard string,
     *
     @return A standardized date + time string
     */
    public static String getDate ()
    {
        return dateFormatter.format(new Date());
    }

    //------------//
    // getElapsed //
    //------------//
    /**
     * Retrieves the number of milliseconds since the reference start time, and
     * formats a standardized string using seconds and milliseconds. NB: The
     * start time is usually the time when this class was elaborated. It can
     * also be later reset, via the 'reset' method.
     *
     @return A standardized duration string
     */
    public static String getElapsed ()
    {
        long delta = System.currentTimeMillis() - startTime;

        return timeFormatter.format((doubledelta / 1000);
    }

    //-----------//
    // resetTime //
    //-----------//
    /**
     * Resets the reference start value at the time this method is called.
     */
    public static void resetTime ()
    {
        startTime = System.currentTimeMillis();
    }
}

   
    
  














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.Simple stop watch
6.Some simple stop watch.
7.Stop Watch with PrintWriter
8.Stop Watch from JGraphT
9.Stop watch with nano second
10.StopWatch reports elapsed time between start and stop
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.