Timer misbehaves if user changes system clock

Can I write a java 'stopwatch' application that records the time between two events, even if the user changes the system clock in between?

I have implemented a short test program because I discovered that different JVMs (Windows <-> Linux) behaved different.
e.g.: if you change the system clock the nanotimer is stable in Windows but not in Linux. (stable means that the nanotime reports the correct elapsed time no matter if the system clock has changed).
Use the test program to check which version is reliable for you OS and your JVM.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.TimerTask;
public class TimingTest
    public static void checkSleepTest()
        long lastTimestamp = System.currentTimeMillis();
        long lastNano = System.nanoTime();
        System.out.println("Start waiting with Thread.sleep(). Reset the system timestamp.");
        while (true)
            try
                Thread.sleep(5000);
            catch (InterruptedException exp)
                System.err.println(exp);
            long time = System.currentTimeMillis();
            long nano = System.nanoTime();
            Date date = new Date(time);
            long dtime = time - lastTimestamp;
            long dnano = nano - lastNano;
            System.out.println("Timer: " + time + " " + date);
            System.out.println("dMillis: " + dtime / 1000d + " dNanos: " + dnano / 1000000000d);
            lastTimestamp = time;
            lastNano = nano;
    public static void checkWaitTest()
        Object lock = new Object();
        long lastTimestamp = System.currentTimeMillis();
        long lastNano = System.nanoTime();
        System.out.println("Start waiting with Object.wait(). Reset the system timestamp.");
        while (true)
            synchronized (lock)
                try
                    lock.wait(5000);
                catch (InterruptedException exp)
                    System.err.println(exp);
            long time = System.currentTimeMillis();
            long nano = System.nanoTime();
            Date date = new Date(time);
            long dtime = time - lastTimestamp;
            long dnano = nano - lastNano;
            System.out.println("Timer: " + time + " " + date);
            System.out.println("dMillis: " + dtime / 1000d + " dNanos: " + dnano / 1000000000d);
            lastTimestamp = time;
            lastNano = nano;
    public static void startUtilTimerTest()
        java.util.Timer timer = new java.util.Timer();
        TimerTask repeatTask = new TimerTask()
            private long lastTimestamp = System.currentTimeMillis();
            private long lastNano = System.nanoTime();
            @Override
            public void run()
                long time = System.currentTimeMillis();
                long nano = System.nanoTime();
                Date date = new Date(time);
                long dtime = time - lastTimestamp;
                long dnano = nano - lastNano;
                System.out.println("Timer: " + time + " " + date);
                System.out.println("dMillis: " + dtime / 1000d + " dNanos: " + dnano / 1000000000d);
                lastTimestamp = time;
                lastNano = nano;
        timer.scheduleAtFixedRate(repeatTask, 1000, 5000);
        System.out.println("Timer has been started. Reset the system timestamp.");
    public static void startSwingTimerTest()
        ActionListener actionTask = new ActionListener()
            private long lastTimestamp = System.currentTimeMillis();
            private long lastNano = System.nanoTime();
            public void actionPerformed(ActionEvent e)
                long time = System.currentTimeMillis();
                long nano = System.nanoTime();
                Date date = new Date(time);
                long dtime = time - lastTimestamp;
                long dnano = nano - lastNano;
                System.out.println("Timer: " + time + " " + date);
                System.out.println("dMillis: " + dtime / 1000d + " dNanos: " + dnano / 1000000000d);
                lastTimestamp = time;
                lastNano = nano;
        javax.swing.Timer timer = new javax.swing.Timer(5000, actionTask);
        timer.start();
        System.out.println("Timer has been started. Reset the system timestamp.");
        // swing timer is a deamon thread => do something else to prevent the program to exit
        while (true)
            try
                Thread.sleep(5000);
            catch (InterruptedException exp)
    public static void main(String[] args)
        if ("swing".equals(args[0]))
            startSwingTimerTest();
        else if ("util".equals(args[0]))
            startUtilTimerTest();
        else if ("sleep".equals(args[0]))
            checkSleepTest();
        else if ("wait".equals(args[0]))
            checkWaitTest();
        else
            System.err.println(args[0] + " not supported.");
}

Similar Messages

  • Install changes system clock

    how come when i install arch, and then boot into windows later, my system clock is changed to like 8 hours ago.  has this happened to anyone before?

    And what time is it really ?
    What I would do now : decide for yourself if you want the CMOS clock in localtime or not. Your decision will probably depend on your Windows system. If you have WIndows98 (first edition) or earlier, you have no choice: CMOS should be localtime. If you have Win98SE or later then carefully check in your Windows system how you set it up. It *usually* wants it in localtime, but it can handle UTC, therefore check your configuration.
    Now, you know how you want your CMOS/BIOS clock to be.
    Now boot into Arch and edit /etc/rc.conf accordingly. Note, however that your TIMEZONE is incorrect. You probably want something like
    TIMEZONE=America/New_York
    TIMEZONE=EST
    Note, that the first automatically switches between daylight savings time, whereas the second does not necessarily. The value you give to TIMEZONE has to actually exist as a file in /usr/share/zoneinfo/ (I.e. no matter weather you live in Boston, D.C., Miami, Atlanta, Philladelphia you always choose America/New_York) !
    Now that you have edited /etc/rc.conf correctly, I would boot into the BIOS and set the CMOS clock there correctly.
    EDIT: links to 2 previous threads about this topic :
    http://bbs.archlinux.org/viewtopic.php?t=2198
    http://bbs.archlinux.org/viewtopic.php?t=830

  • Timer problems on Linux changing system time and data

    On Linux SO,
    both with flash player plugin and standalone flash player, setting system time with command:
    date -s 11:11:11
    hang timers (flash.utils.Timer) immediately!

    I notice that updating system time (both with "date -s hh:mm:ss" and through ntpd) with a new time in the future doesn't hang up flash.utils.Timer component.
    Contrary, if I update system time with a past new one, flash.utils.Timer stops work immediately!!!
    This is huge problem for those interfaces that need to run for a long time and also need to be time updated (for examplem through ntp daemon).
    Can anyone help me?
    PS: Windows Flashplayer doesn't have this problem !!

  • ICal, Mail and Finder times all stuck since upgrade!  System clock O.K.

    Very strange problem:
    All of my calendar event times (in iCal), email message receipt times (in Mail), and file modification/creation times (in Finder) are listed as some time between 7:00-7:59 AM or PM, regardless of time of day! My system clock (as it appears in the menu bar) is fine!
    More details:
    iCal: the events still look like they're at the right times, i.e. the blocks are in the right places, with the correct spacing, etc etc, they are just labeled with times between 7:00-7:59 AM/PM. Similarly, alarms go off at the correct time. Also, in the day or week listing, the column on the left which is supposed to show times instead just says "Sat" for all times, except "Noon" (at noon).
    This is continuous since upgrading to snow leopard a couple weeks ago. Seemingly no other problems besides this.
    I don't know much about the OS, but I don't understand how the system clock (in the menu bar) could be fine, but every other sign of time-keeping is completely off. I just its just something about the labeling of times, instead of the actual record of them?!
    I have screen shots of the problems if that might help clarify (apparently I can't attach them however).
    Thanks for your help!

    Hi,
    Any solutions to your problems?
    Just found my Mom has the same 'time stuck' problem (iMac OS X 10.6.7) Finder 'Date Modified' is stuck at 2/14/2011. Also, the Time/Date displays correctly on the Menu bar but when click on it, the drop down menu shows a gray out 2/14/2011, etc.
    Much appreciated of any help. Thx.
    -MasterMoo

  • System clock is constantly inaccurate - Sat Pro U400

    Salaam :-)
    My laptop system clock is constantly inaccurate. I have windows 7 on Satellite Pro U400-13J. Whenever I glance at the clock down left on the taskbar I see it way before correct time. I have this laptop since about 18 months, does this point to a dead battery leading to a CMOS failure ?

    Selam aleykum,
    Your laptop is still new and the cmos battery shouldn't be dead. I advise you:
    1 leave laptop charging for 48 hours to charge cmos battery full.
    2. If your system has an always on broadband internet connection, you can automate the process by changing the set interval between updates.
    Open up the registry editor by choosing Start -> Run -> Regedit
    Find the registry key HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\
    Services\W32Time\TimeProviders\NtpClient
    Select the entry for SpecialPollInterval and change the decimal value to a period of time you wish to use. The data value is in seconds, so for example, a value of 172800 will change the synchronization interval to 2 days, a value of 86400 will change it to 1 day, or a value of 3600 will change it to one hour.
    Exit from the Registry Editor, and ensure the Automatically synchronise option is ticked for your system clock settings. (Be sure you use Apply when youve finished making changes to the time setting box for the system clock, so the change there is a permanent one.)
    http://img821.imageshack.us/img821/8924/registrytwaek.jpg
    I have placed my registry printscreen where enter 3600 ( it means every hour your system clock will sychronize with time server but you have always connected to Internet)

  • Change In Clock Time

    Hello All,
    We are working in UK. We have SAP for a system running on  ECC6 on AIX with TL07 SP1, and another running on 4.7 on AIX with TL08 SP3. We have to plan to change the clock by this month end. Last time we had an issue for the same and were getting the LARGE TIME DIFFERENCE dumps.
    We had to apply SP2 for the 4.7 system as it was running on TL07 SP1. Now, I would like to know if we face any problem in changing the time. Please help.
    Thanks,
    Nick.S

    - your system should not travel backwards in time
    - the time should be consistent across your environment
    Best practice is to stop the system during the time conversion.
    Have a look at these notes:
    [102088 - Reducing downtime when changing from summer to winter time|https://service.sap.com/sap/support/notes/102088]
    [398374 - Problems when converting summer time <-> winter time|https://service.sap.com/sap/support/notes/398374]
    Best regards, Michael

  • Hi add fields in ztable user name system time and date urgent

    i have to add three new fields in my z table
    a)user name
    b)system time
    c)date
    suppose if anyone update my ztable then the above three fields should catch the values as which user change the ztable at what time and on which date
    plzzzzzz help
    urgent
    saurabh

    To create the fields use the following field types.
    CPUTD - for Date
    CPUTM - for Time
    USNAM - User name.
    when updating the table pass the system variable.
    sy-datum - for date
    sy-uzeit - for time
    sy-uname - for user name

  • Is it acceptable for a developer to tell a user to tamper with the system clock?

    Im not sure where to post this question but i cant find a a way to email Apple with this question. I need an official response from Apple on whether or not it is acceptable for a developer to tell a user to tamper and change the system clock in order to use their app.

    I am actually the user. A game developer told me to do this in order to play their game temporarily because their update had a glitch. Their official response to their game forums was change date and play through the weekend until they could apply a patch.  Well, in turn it messed up my daughters game because her game is a progression style game which looks at the clock. I need something somewhere that says, tampering with the clock is not approved. Only way to bring my daughters game back to current progression is to buy "diamonds" because it is a "freemium" type game. Developer that told me to change clock basically told me "tough luck, go away" andi want to take them to court for about 800$ to buy appropriate amount of diamonds.

  • Boot fail after system clock changed

    Hi!
    I've changed the system clock in BIOS, after that I've got an error message.
    Grub was loading until reached the /dev/sda3 check and suddenly stopped and asked the root pass.
    Unfortunately I wasn't able to type in my pass because of hu_HU charset.
    After that I restored the original date and now everything is fine except the date (At the moment the date is: 2009 Monday 17:35:13)
    How Can I set up the correct date and what was that error?
    I appreciate every help.
    Thx.

    MadTux wrote:Did you see an error message? Could have been a problem with the file system. I guess if the date in the file system is before the current date, this could cause problems. Why not use ntp? Then you always have the correct time.
    That was the problem I guess. I don't know ntp. I was wondering if you'd tell me more about ntp.

  • Time dead after changing system time

    Hi,
    I have read many posts on this but still can't find a perfect solution.
    1. tried using thread.sleep(1000) to cause a method to run every 1 sec.. but after reading online.. it seems to be a lousy design as it depends on the system cpu speed. sometimes it will stop for more than 1 sec.
    2. tried :
    private ScheduledExecutorService scheduler
    private ScheduledFuture task
    public classname(){
    scheduler = Executors.newSingleThreadScheduledExecutor();
    task = scheduler.scheduleAtFixedRate(methodToRun,1,1,TimeUnit.SECONDS)
    This works but if i changed the system time for multiple times, it will just stop my program...
    Did i use ScheduledExecutorService or thread right ?

    after reading online.. it seems to be a lousy design as it depends on the system cpu speed.God help us! Where on earth did you read that? It depends on the system clock, and it isn't guaranteed to retun in exactly the time specified.
    sometimes it will stop for more than 1 sec.That's what its specification says.
    This works but if i changed the system time for multiple times, it will just stop my program...No, it won't 'stop' your program, it will foul up the executor until it adjusts to the new time.
    There is nothing in standard Java that guarantees exact timing. If you need that you need a real-time version of Java. But probably you don't need that at all. Impossible to say without some context.

  • How to Sync System Clock with Internet Time in Windows 7?

    "Internet Time" tab is missing in Clock Settings as shown in below link.
    http://mintywhite.com/windows-7/7maintenance/windows-seven-7-sync-system-clock-with-internet-time-how-to/
    Please suggest how to Sync System Clock with Internet Time in Windows 7?
    Regards, Dharmendra, Bangalore (India)

    The note at the bottom of the first link from Alex Verboon has the following, which worked on my Domain-associated Windows 7 system:
    Synchronizing with an Internet time server
    You can synchronize your computer clock with an Internet time server. This means that the clock on your computer is updated to match the clock on the time server, which can help ensure that the clock on your computer is accurate. Your clock is typically updated
    once a week and needs to be connected to the Internet for the synchronization to occur.
    Note
    If your computer is on a domain, you can't synchronize your clock with an Internet time server.
    Open Date and Time by clicking the Start button, clicking Control Panel, clicking Clock, Language, and Region, and then clicking Date and Time.
    Click the Internet Time tab, and then click Change settings. If you are prompted for an administrator password or confirmation, type the password or provide confirmation.
    Click Automatically synchronize with an Internet time server, select a time server, and then click OK.

  • System clock keeps changing on KT400 MS 6590

    Hi Guys,
    I'm not sure why but my system clock seems to change. The problem isn't every time I log into Windows XP but at random. If I restart then the clock goes to the correct time again. Is there any way of correcting this other than changing the times in Windows and the BIOS. I have also tried clearing the COMS, but this hasn't worked either.
    Thanks for any help you can provide.
    Richard.

    99 percent of boards are a cr2032
    any big chemist,camera shop,most corner shops
    pop the old one out
    there is a little tag
    put new in make sure its same way round
    you might find bios is reset and needs resetting

  • How to Enforce User Change Password First Time User in Release 2?

    Hi...
    We discovered in Oracle Directory Manager(in unix is oidadmin), there actualy
    column to expiry date.(the default is 60).
    We follow this notes in metalink..
    Note:176470.1 Subject: How To Pre-Expire Portal Passwords
    Even though the note is for Portal30(release1), we just wangt to try it in our Release 2...
    Extract from the note
    "3. Set the value of the column LAST_PASSWD_CHANGE_TIME in the table WWSEC_PERSON$ in SSO schema to a value older than the password expiration period (default is 90 days) before the current time (e.g. sysdate - 100) for the appropriate user(s)
    4. For example, if you have created a user called TEST
    you would issue a command such as:
    update wwsec_person$
    set LAST_PASSWD_CHANGE_TIME =sysdate-70
    where USER_NAME='TEST';
    commit; "
    But, when we try to login again in the portal, we can still login...
    So, is the function still exist in Release 2?..
    If not, why Oracle Portal throw that function away?
    Why there still columns in WWSEC_PERSON$ that maybe linked to the password problem?
    can anybody help/explain?
    Thanks....

    Rather than rehash what has already been thoroughly discussed, check this thread. This should answer your question.
    Can a user change his own password after admin has set it
    The english version of Jose Troya's blog: http://obiee101.blogspot.com/2008/08/obiee-change-password.html
    Edited by: LC143 on Aug 27, 2008 1:36 PM

  • How to change system time through java program

    Hi
    I want to know, how to change system time through java program.
    give me a idia with example.
    Thanks

    There isn't any core Java API for this. Use JNI or call an external process with Runtime.exec().
    ~

  • How to change system time on Cisco MeetingPlace Express Server

    How to change system time on Cisco MeetingPlace Express Server
    Model MCS-7825-I4

    Hi Vijay,
    As per the install guide for MPX 1.x as well as 2.x "Caution! Be sure you enter the correct date and time. You must reinstall the Cisco Unified MeetingPlace Express operating system and application if you need to change the date or time of your server in the future."
    http://docwiki.cisco.com/wiki/Cisco_Unified_MeetingPlace_Express,_Release_2.x_--_Configuring_the_Cisco_Unified_MeetingPlace_Express_Server#Configuring_the_Cisco_Unified_MeetingPlace_Express_Server
    http://www.cisco.com/c/en/us/td/docs/voice_ip_comm/meetingplace_express/1_2/english/installation/guide/iug/mpxinst3.html
    HTH
    Manish

Maybe you are looking for

  • Material Order status report - Closed PO's

    Hi, In my "Material Order status report" I got closed PO's. I dont want any closed PO's to be displayed in my report. How to set this? Regards P.Ramprakash

  • Imac HDMI doesn't display to TV through HDMI

    imac HDMI doesn't display to TV through HDMI switch

  • Isight driver install problems

    Hello all, well after installing SL then Win 7 then running bootcamp inside windows almost everything is working correctly. The only error i'm faced with is the iSight drivers do not want to correctly install. At boot up i get a driver install error

  • No option for 1080p youtube

    Hi, i have always had the option to play youtube videos in 1080 or higher until today. for some reason when i try to play a video that IS 1080p, i don't even get the option to change to anything higher then 720p. i also tried this with Explorer and i

  • Can't get past Darwin console

    I just installed some Quicktime add-ons, and I restarted my Mac. It goes to a Darwin console, I can log in to the console, if I exit, it takes me back onto the startup user menu, and if I select my user account, I'm right back into Darwin, being prom