Class encapsulating timer functionality : Timer « Development Class « Java

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 Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Development Class » TimerScreenshots 
Class encapsulating timer functionality
  
/*
 * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
 
 * Project: OpenSubsystems
 
 * $Id: MyTimer.java,v 1.5 2007/01/07 06:14:00 bastafidli Exp $
 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License. 
 
 * This program 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 General Public License for more details.
 
 * You should have received a copy of the GNU 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 
 */


/**
 * Class encapsulating timer functionality.
 
 @version $Id: MyTimer.java,v 1.5 2007/01/07 06:14:00 bastafidli Exp $
 @author Miro Halas
 * @code.reviewer Miro Halas
 * @code.reviewed Initial revision
 */
public class MyTimer
{
   // Attributes ///////////////////////////////////////////////////////////////
   
   /**
    * Remeber start time here.
    */
   private long m_lStartTime;
   
   /**
    * Remeber stop time here.
    */
   private long m_lStopTime;

   // Constructors /////////////////////////////////////////////////////////////
   
   /**
    * Default constructor.
    * Starts counting fro the moment it is cosntructed.
    */
   public MyTimer(
   )
   {
      reset();
   }

   // Logic ////////////////////////////////////////////////////////////////////
   
   /**
    @return long - start time;
    */
   public long getStartTime()
   {
      return m_lStartTime;
   }

   /**
    @return - stop time;
    */
   public long getStopTime()
   {
      return m_lStopTime;
   }

   /**
    * Reset the counter and start counting from scratch.
    */
   public void reset(
   )
   {
      m_lStartTime = System.currentTimeMillis();
      m_lStopTime = 0;
   }

   /**
    * Stop the timer.
    */
   public void stop(
   )
   {
      m_lStopTime = System.currentTimeMillis();
   }

   /**
    * Get timer duration (the timer doesn't stop) in milliseconds.
    
    @return long - difference between stop and start time.
    */
   public long getDuration(
   )
   {
      long lStopTime;

      if (m_lStopTime == 0)
      {
         lStopTime = System.currentTimeMillis();
      }
      else
      {
         lStopTime = m_lStopTime;
      }

      return lStopTime - m_lStartTime;
   }

   /**
    * Print the state of the timer without stopping it.
    @return String - timing information
    */
   public String toString(
   )
   {
      long lTotalMS   = getDuration();
      long lMS        = lTotalMS % 1000;
      long lTotalSecs = lTotalMS / 1000;
      long lSecs      = lTotalSecs % 60;
      long lTotalMins = lTotalSecs / 60;
      long lMinutes   = lTotalMins % 60;
      long lHours     = lTotalMins / 60;
      StringBuffer sbBuffer = new StringBuffer();

      if (lHours > 0)
      {
         sbBuffer.append(lHours);
         sbBuffer.append(":");
         sbBuffer.append(lMinutes);
         sbBuffer.append(":");
         sbBuffer.append(lSecs);
         sbBuffer.append(".");
         sbBuffer.append(lMS);
      }
      else if (lMinutes > 0)
      {
         sbBuffer.append(lMinutes);
         sbBuffer.append(":");
         sbBuffer.append(lSecs);
         sbBuffer.append(".");
         sbBuffer.append(lMS);
      }
      else if (lSecs > 0)
      {
         sbBuffer.append(lSecs);
         sbBuffer.append(".");
         sbBuffer.append(lMS);
         sbBuffer.append(" seconds");
      }
      else
      {
         sbBuffer.append(lMS);
         sbBuffer.append(" ms");
      }
      
      return sbBuffer.toString();
   }
}

   
    
  
Related examples in the same category
1. Timer Skipping BeepTimer Skipping Beep
2. Timer Schedule a task that executes once every secondTimer Schedule a task that executes once every second
3. Use java.util.Timer to schedule a task to execute once 5 seconds have passedUse java.util.Timer to schedule a task to execute once 5 seconds have passed
4. Timer utilities
5. Timer and TimerTask Classes
6. Pause and start a timer task
7. Create a Timer object
8. Swing also provide a Timer class. A Timer object will send an ActionEvent to the registered ActionListener.
9. Schedule a task by using Timer and TimerTask.
10. Scheduling a Timer Task to Run Repeatedly
11. Create a scheduled task using timer
12. extends TimerTask to create your own task
13. A simple implementation of the Java 1.3 java.util.Timer API
14. Scheduling a Timer Task to Run at a Certain Time
15. Timeout Observer
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.