Basic Timer : 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.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java Tutorial
Java Book
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
SCJP
Java » Development Class » StopWatchScreenshots 
Basic Timer
  


import java.util.*;

/**
 @author Matthew D. Hicks
 *
 */
public class BasicTimer {
  public static final int TYPE_CLOCK = 1;
  public static final int TYPE_ELAPSED = 2;
  public static final int TYPE_REMAINING = 3;

  private int type;
  private long start;
  private long max;
  
  public BasicTimer(int type) {
    this.type = type;
    start = System.currentTimeMillis();
  }
  
  public long getTime() {
    if (type == TYPE_CLOCK) {
      return System.currentTimeMillis();
    else if (type == TYPE_ELAPSED) {
      return System.currentTimeMillis() - start;
    else if (type == TYPE_REMAINING) {
      return max - (System.currentTimeMillis() - start);
    }
    throw new RuntimeException("Type does not match anything known: " + type);
  }
  
  public void setMax(long max) {
    this.max = max;
  }
  
  public static void main(String[] argsthrows Exception {
    int type = TYPE_REMAINING;
    BasicTimer timer = new BasicTimer(type);
    timer.setMax(10000);
    GregorianCalendar calendar = new GregorianCalendar();
    while (true) {
      Thread.sleep(1000);
      if (type == TYPE_CLOCK) {
        calendar.setTimeInMillis(timer.getTime());
        System.out.println("Clock: " + calendar);
      else if (type == TYPE_ELAPSED) {
        System.out.println("Elapsed: " (timer.getTime() 1000" seconds");
      else if (type == TYPE_REMAINING) {
        System.out.println("Remaining: " (timer.getTime() 1000" seconds");
      }
    }
  }
}

   
    
  
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.Provides various features related to the current date and timeProvides various features related to the current date and time
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.
w_ww.ja__v__a_2s__.__c___o__m__ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.