Holger's
Java API

com.antelmann.calendar
Class AtomicClock

java.lang.Object
  extended by com.antelmann.calendar.AtomicClock
All Implemented Interfaces:
TimeSystem

public class AtomicClock
extends Object
implements TimeSystem

uses the Network Time Protocol (NTP). This simple implementation is based on a single server.

Author:
Holger Antelmann
See Also:
NTPServer, AtomicTime

Field Summary
static int DEFAULT_NTP_PORT
          37
static String DEFAULT_NTP_SERVER
          time-b.nist.gov
protected static String[] knownServerList
          a small list of known servers besides the default one
 
Fields inherited from interface com.antelmann.calendar.TimeSystem
systemTime
 
Constructor Summary
AtomicClock()
          uses time-b.nist.gov
AtomicClock(String serverName)
          uses the standard port 37
AtomicClock(String serverName, int port)
           
 
Method Summary
 int adjustDelta()
          adjusts the delta value used for getTime() through using the server time
 TimerTask createTimerTask(Logger logger)
          creates a TimerTask that will adjust the delta and log the result to the given logger.
 int currentDelta()
          returns the current difference between system time and atomic time in milliseconds.
 long currentTimeMillis()
          returns the system time adjusted by the delta value.
 long currentTimeMillisOnline()
          returns the current time in the current locale retrieved directly from the server.
 Calendar getCalendar()
          returns the calendar used to localize the time
 int getDelta()
          returns the stored difference between system time and atomic time in milliseconds.
static String[] getKnownServerList()
           
 long getLastDeltaAdjustmentTime()
          returns the system time when adjustDelta() has been successfully called last time.
 int getPort()
           
 String getServer()
           
 int getTimeout()
           
static void main(String[] args)
          console based demo app displaying the delta between the system time and the server
protected  int secondsSince1900()
          returns the raw int directly from the server
 void setDelta(int delta)
          manually sets the value used to adjust system time
static void setKnownServerList(String[] newList)
           
 void setPort(int port)
           
 void setServer(String serverName)
           
 void setTimeout(int timeout)
           
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_NTP_SERVER

public static final String DEFAULT_NTP_SERVER
time-b.nist.gov

See Also:
Constant Field Values

DEFAULT_NTP_PORT

public static final int DEFAULT_NTP_PORT
37

See Also:
Constant Field Values

knownServerList

protected static String[] knownServerList
a small list of known servers besides the default one

Constructor Detail

AtomicClock

public AtomicClock()
uses time-b.nist.gov


AtomicClock

public AtomicClock(String serverName)
uses the standard port 37


AtomicClock

public AtomicClock(String serverName,
                   int port)
Method Detail

getKnownServerList

public static String[] getKnownServerList()

setKnownServerList

public static void setKnownServerList(String[] newList)

getServer

public String getServer()

setServer

public void setServer(String serverName)

getPort

public int getPort()

setPort

public void setPort(int port)

setTimeout

public void setTimeout(int timeout)

getTimeout

public int getTimeout()

getCalendar

public Calendar getCalendar()
returns the calendar used to localize the time


currentTimeMillis

public long currentTimeMillis()
returns the system time adjusted by the delta value. This is generally the prooper method to be called by the application.

Specified by:
currentTimeMillis in interface TimeSystem
Returns:
the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.

secondsSince1900

protected int secondsSince1900()
                        throws IOException
returns the raw int directly from the server

Throws:
IOException

currentTimeMillisOnline

public long currentTimeMillisOnline()
                             throws IOException
returns the current time in the current locale retrieved directly from the server. Calling this method does not affect the delta value of this instance.

Throws:
IOException

currentDelta

public int currentDelta()
                 throws IOException
returns the current difference between system time and atomic time in milliseconds. A negative number indicates that the system clock is behind. Calling this method does not affect the delta value of this instance.

Throws:
IOException

getDelta

public int getDelta()
returns the stored difference between system time and atomic time in milliseconds. A negative number indicates that the system clock is behind.

See Also:
adjustDelta(), setDelta(int)

setDelta

public void setDelta(int delta)
manually sets the value used to adjust system time

See Also:
adjustDelta()

getLastDeltaAdjustmentTime

public long getLastDeltaAdjustmentTime()
returns the system time when adjustDelta() has been successfully called last time. If no sucessful adjustment has taken place, yet, the method returns 0.

See Also:
adjustDelta()

adjustDelta

public int adjustDelta()
                throws IOException
adjusts the delta value used for getTime() through using the server time

Throws:
IOException
See Also:
currentTimeMillis(), getDelta(), setDelta(int), getLastDeltaAdjustmentTime()

createTimerTask

public TimerTask createTimerTask(Logger logger)
creates a TimerTask that will adjust the delta and log the result to the given logger. The logger may be null.

See Also:
adjustDelta(), Timer

toString

public String toString()
Overrides:
toString in class Object

main

public static void main(String[] args)
                 throws IOException
console based demo app displaying the delta between the system time and the server

Throws:
IOException


(c) 2001-2006 Holger Antelmann - all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads