Anyone know exactly how Date works?

After several postings to the long and rather fruitless thread about Date, I thought I'd try a little test for myself to see how/whether Java handles leap seconds.
Here's my code, based on Wikipedia, which says that the last UTC leap second was added on 2008/12/31:
public static void main(String... args) {
   TimeZone utc = TimeZone.getTimeZone("UTC");
   SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS z");
   sdf.setTimeZone(utc);
   long t = 1230768000000L; // 2009/1/1 00:00:00 UTC
   for (int s = -2; s < 3; s++) {
      Date instant = new Date(t + (s * 1000L));
      System.out.println(sdf.format(instant));
}which produces:
2008/12/31 23:59:58.000 UTC
2008/12/31 23:59:59.000 UTC
2009/01/01 00:00:00.000 UTC
2009/01/01 00:00:01.000 UTC
2009/01/01 00:00:02.000 UTC
The results surprised me, because it tends to suggest that Date's "ticker" (like Unix's) is not linear, but adjusts before/after the leap second to maintain a minute that is always 60 seconds. I also double-checked it with 31/12/1995 (which is listed in Date's documentation) and it produces similar results.
As a 3rd test, I added:
   GregorianCalendar gc1 = new GregorianCalendar(utc);
   gc1.set(1995, 11, 31, 23, 59, 58);
   gc1.set(Calendar.MILLISECOND, 0);
   GregorianCalendar gc2 = new GregorianCalendar(utc);
   gc2.set(1996, 0, 1, 0, 0, 2);
   gc2.set(Calendar.MILLISECOND, 0);
   System.out.println(String.format(
      "The difference between %s and %s is %d milliseconds",
         sdf.format(gc1.getTime()),
         sdf.format(gc2.getTime()),
         gc2.getTime().getTime() - gc1.getTime().getTime()
   ));and this produced:
The difference between 1995/12/31 23:59:58.000 UTC and 1996/01/01 00:00:02.000 UTC is 4000 milliseconds.
( which it isn't :-) )
I'd be happy for anyone to tell me if I've made a mistake, but the above results suggest to me that neither of Java's main time classes are completely safe for calculating intervals of time. Does this go along with other people's findings?
Also, does anybody know if this is machine-dependent? It was run on a Dell AMD Athlon running Vista.
Winston

YoungWinston wrote:
After several postings to the long and rather fruitless thread about Date, I thought I'd try a little test for myself to see how/whether Java handles leap seconds.
You seek implementation details of java.util.Date. Why don't you simply look at the source or at least start there.
Here's my code, based on Wikipedia, which says that the last UTC leap second was added on 2008/12/31:
public static void main(String... args) {
TimeZone utc = TimeZone.getTimeZone("UTC");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS z");
sdf.setTimeZone(utc);
long t = 1230768000000L; // 2009/1/1 00:00:00 UTC
for (int s = -2; s < 3; s++) {
Date instant = new Date(t + (s * 1000L));
System.out.println(sdf.format(instant));
}which produces:
2008/12/31 23:59:58.000 UTC
2008/12/31 23:59:59.000 UTC
2009/01/01 00:00:00.000 UTC
2009/01/01 00:00:01.000 UTC
2009/01/01 00:00:02.000 UTC
The results surprised me, because it tends to suggest that Date's "ticker" (like Unix's) is not linear, but adjusts before/after the leap second to maintain a minute that is always 60 seconds. I also double-checked it with 31/12/1995 (which is listed in Date's documentation) and it produces similar results.
As a 3rd test, I added:
   GregorianCalendar gc1 = new GregorianCalendar(utc);
gc1.set(1995, 11, 31, 23, 59, 58);
gc1.set(Calendar.MILLISECOND, 0);
GregorianCalendar gc2 = new GregorianCalendar(utc);
gc2.set(1996, 0, 1, 0, 0, 2);
gc2.set(Calendar.MILLISECOND, 0);
System.out.println(String.format(
"The difference between %s and %s is %d milliseconds",
sdf.format(gc1.getTime()),
sdf.format(gc2.getTime()),
gc2.getTime().getTime() - gc1.getTime().getTime()
));and this produced:
The difference between 1995/12/31 23:59:58.000 UTC and 1996/01/01 00:00:02.000 UTC is 4000 milliseconds.
( which it isn't :-) )
I'd be happy for anyone to tell me if I've made a mistake, but the above results suggest to me that neither of Java's main time classes are completely safe for calculating intervals of time. Does this go along with other people's findings?
Are you concerned that if you compute an interval of say, 1000 milliseconds, exactly 1000 milliseconds may not have actually elapsed? My guess is the answer is likely to be found in how Date.getTime() comes up with the long value.
Also, does anybody know if this is machine-dependent? It was run on a Dell AMD Athlon running Vista.
Theoretically it should not with Java being platform independent and all. It would most likely depend though on where and how Date comes up the long value in the host environment. I honestly don't know any implementation details though.

Similar Messages

  • I have a 3GS. Am trying to sync a plantronics explorer 232 Bluetooth headset and can't figure it out. I have Bluetooth turned on but it searches for devices and the headset never shows up. Anyone know exactly how to do this?  My tech skills are lacking

    I have a 3GS. Am trying to sync a plantronics explorer 232 Bluetooth headset and can't figure it out. I have Bluetooth turned on but it searches for devices and the headset never shows up. Anyone know exactly how to do this?  My tech skills are lacking

    I have the same blueparrott headset headset with the directions. Actually reading them just fixed my iphone pairing issue.
    Here they are in case u still need them along with what I needed to do to get mine to work with iphone 4
    Pair Headset with Phone
    Turn off headset off
    Hold down headset's MFB (large top button) until red & blue lights flash and you hear 2 rising tones, followed by 4 more tones. Must wait for all before you release the MFB button!! (I had my hubby hold the MFB while I waited for the tones since both come from the headset
    Release the MFB and follow your phone's instructions to place it in 'bluetooth' discoverable mode.  With the iphone4, go to "General" and then "Bluetooth" and turn indicator to "ON".
    This is where my issue occurred as the phone could not find the bluetooth, although it worked with my LG phone.  So after trying it several times thinking I was nuts and that the issue was with the iphone, I looked further in the blueparrot's directions and found this step that fixed the issue.
    Reset Paired Devices List
    On the bluetooth, press and hold down both the volume up & volume down buttons for 5-6 seconds.  A double beep will be heard and the list will be reset.  Pairing info for devices previously paired will be lost.
    The bluetooth directions provide this as step 4 but that did not work for my phone (see below)
    4. Once the phone discovers the headset, select "pair" and enter "0000" code and that when pairing is complete the blue light on the headset will stop flashing and phone will prompt you to 'connect'.
    This is what I had to do after resetting the device list on my bluetooth
    4. My iphone instanting discovered the headset and displayed it under 'Devices" but showed a message that it was 'not connected'.  Once I selected the device on the iphone by pressing on it then it changed to "connected". 
    Even when I turn the bluetooth on and off the device remains on the list and alll I do is select it to connect and disconnect after I turn on my blueparrott bluetooth
    I hope this helps others!   

  • My ipod works perfectly well when connected to my laptop, but won't even switch on when it's stand alone. Battery is showing as fully charged. Anyone know if/how I can fix this?

    Hi, my ipod nano works perfectly well when connected to my laptop, but won't even switch on when it's stand alone. Battery is showing as fully charged. Anyone know if/how I can fix this? Thanks

    Take the iPod  N ano to your local Apple Store or an AASP to be examined. 

  • Anbody know exactly how fast fresfresh works (i.e. if row by row)?

    Hi there,
    Anbody know exactly how fast fresfresh works (i.e. if row by row).
    I've read on other sites that fast refresh works on a row-by-row-basis as opposed to set-based
    Is ths true?
    Many Thanks

    Hi,
    I do not think the processing is row by row…
    You can check it for yourself – trace a session that issues a fast refresh and examine the trace file.
    I did that exercise back on Oracle 8i, and got “insert as select “ statement (one statement, not a statement for every row!) that uses “MLOG$_” MV log table to retrieve the changed records.
    Hope that helps,

  • Does anyone know whether Numbers will work on the MabBook Air?, Does anyone know whether Numbers will work on the MabBook Air?

    Does anyone know if Numbers will work on the MacBook Air?

    If you just downloaded "free" Numbers then yes you have the current version that runs on OS X v9.x 
    If you need a Mac data processor (not specifically Numbers) then Open Office.org, Neo Office.org both have spread sheet (aka "number like) apps included with them. They also are free.

  • I am using iplanet messaging 5.2. I want to send mail using command line. Anyone knows exact command line syntax.

    I am using iplanet messaging 5.2. I want to send mail using command line. Anyone knows exact command line syntax.
    Thanks
    -Vasu

    #telnet 0 25
    mail from: user1 #email source
    rcpt to: [email protected] #email destination
    data #begin email content
    Subject: Hi, This is a test mail #optional subject
    Hi, Please don't pay attention to this email, just a test
    . #end email content
    quit #Quit SMTP
    #

  • Does anyone know if/how to rotate a video clip?  I shot it so that it is sideways on my computer screen and I cant figure out how to rotate it.

    Does anyone know if/how to rotate a video clip in iphoto?  I shot it so that it plays sideways on my computer and I cannot figure out how to rotate it.  Thanks.

    You can't do that in iPhoto. You'll need a video editor like iMovie for the job.
    Regards
    TD

  • Does anyone know to how to copy notes from an ipod to an ipad

    Does anyone know to how to copy notes from an ipod to an ipad

    See iPod: Storing and reading notes to extract the existing notes on the iPod.
    If you only need to view them on the device Dropbox would be one way to go.
    tt2

  • I just purchased the Apple MB974ZM/B World Travel Adapter Kit for my iphone 5. It says it works for the 4.  Does anyone know if this will work for my iphone 5?

    I just purchased the Apple MB974ZM/B World Travel Adapter Kit for my iphone 5. It says it works for the 4.  Does anyone know if this will work for my iphone 5?

    here is the link to Apples compatibility guide for it.
    http://store.apple.com/us/product/MB974ZM/B/apple-world-travel-adapter-kit

  • Would like to use iMessages internationally, sending between an iPod touch and and iPhone. Does anyone know if it will work between countries?

    Would like to use iMessages internationally, sending between an iPod touch and and iPhone. Does anyone know if it will work between countries?

    The iPhone is not user servicable. You can get an out of warranty replacement from Apple for $149. If you open the phone yourself, you forfeit the ability to do this.

  • Does anyone know the official date of the iOS 7

    does anyone know the official date of the iOS 7

    KiltedTim wrote:
    Every time someone asks this question here, they move the release back by one hour... Right now, it's sometime in 2017.

  • Re: Does anyone know where the data base

    We do maintain a searchable indexed archive at
    http://pinehurst.sagesoln.com/listarchive. Recently because of Sage office
    moves and other IT intensive activities I've been lax about getting it
    updated. I'll try to get it straightened out in the next week or so.
    Jamie Fox
    Systems Administrator
    Sage IT Partners
    [email protected]
    (415) 399-7210
    David Trudell wrote:
    >
    that contains a history of all the messages sent reside? I tried looking on
    sageit.com, but I couldn't find it.
    Thanks
    David Trudell
    There are two major products to come out of Berkeley: LSD and
    UNIX. We don't believe this to be a coincidence.
    To unsubscribe, email '[email protected]' with
    'unsubscribe forte-users' as the body of the message.
    Searchable thread archive <URL:http://pinehurst.sageit.com/listarchive/>

    David,
            Try this:
                                <a
    href=
    "http://pinehurst.sageit.com/listarchive/">http://pinehurst.sageit.com/listarchive/</a>
    <http://pinehurst.sageit.com/listarchive/>
    -----Original Message-----
    From: David Trudell [mailto:[email protected]]
    Sent: Wednesday, January 20, 1999 2:18 PM
    To: [email protected]
    Subject: Does anyone know where the data base
    that contains a history of all the messages sent reside?  I tried looking on
    sageit.com, but I couldn't find it.
    Thanks
    David Trudell
    There are two major products to come out of Berkeley: LSD and
    UNIX.  We don't believe this to be a coincidence.
    To unsubscribe, email '[email protected]' with
    'unsubscribe forte-users' as the body of the message.
    Searchable thread archive <URL:http://pinehurst.sageit.com/listarchive/>

  • Just set-up my first IPad (Air) using ICloud.  This resulted in none of my desktop Outlook icons working.  Can anyone tell me how ICloud works and restore functional Outlook icons on my desktop.

    Just set-up my first IPad (Air) using ICloud.  This resulted in none of my desktop Outlook icons working.  Can anyone tell me how ICloud works and restore functional Outlook icons on my desktop?

    Just set-up my first IPad (Air) using ICloud.  This resulted in none of my desktop Outlook icons working.  Can anyone tell me how ICloud works and restore functional Outlook icons on my desktop?

  • Does anyone know about how to get the blue light @ the top of the left corner of the Samsung Galazy 3 to quit flashing continuously?

    Does anyone know about how to get the blue light @ the top of the left corner of the Samsung Galazy 3 to quit flashing continuously?

    It would appear you have some sort of notification set up and have yet to look at the app which is sending the notification to the phone.
    You would have to first determine which app is sending the notification and then change the settings in that app to disable notifications if you no longer want to be alerted by that app.

  • Anyone know FCP Studio will work on my Powerbook G4 17?

    hello there
    anyone know FCP Studio will work on my Powerbook G4 17?
    CPU Speed: 1 GHz
    Memory: 1 GB
    Graphics: GeForce4 MX 64MB
    is it CPU too slow or Graphic card will be a problem when running Motion2?
    if forget the speed, does my Powerbook will do the job?
    please inform me, many thanks
      Mac OS X (10.4.7)  

    For me, Same computer just 512 MB RAM, it will
    install everything but Motion from the Install Final
    Cut Studio CD. Graphics Card not supported is the
    message when I try to install it seperately from the
    Motion 2 CD.
    cheers mate!

Maybe you are looking for