Skip to main content

Why is subtracting these two times (in 1927) giving a strange result?


If I run the following program, which parses two date strings referencing times one second apart and compares them:




public static void main(String[] args) throws ParseException {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str3 = "1927-12-31 23:54:07";
String str4 = "1927-12-31 23:54:08";
Date sDt3 = sf.parse(str3);
Date sDt4 = sf.parse(str4);
long ld3 = sDt3.getTime() /1000;
long ld4 = sDt4.getTime() /1000;
System.out.println(ld3);
System.out.println(ld4);
System.out.println(ld4-ld3);
}



The output is:




-1325491905
-1325491552
353



Why is ld4-ld3 not 1 (as I would expect from the one-second difference in the times), but 353 ?



If I change the dates to times a second later:




String str3 = "1927-12-31 23:54:08";
String str4 = "1927-12-31 23:54:09";



Then ld4-ld3 will be 1





UPDATE



Java version:




java version "1.6.0_22"
Java(TM) SE Runtime Environment (build 1.6.0_22-b04)
Dynamic Code Evolution Client VM (build 0.2-b02-internal, 19.0-b04-internal, mixed mode)



Timezone( TimeZone.getDefault() ):




sun.util.calendar.ZoneInfo[id="Asia/Shanghai",
offset=28800000,dstSavings=0,
useDaylight=false,
transitions=19,
lastRule=null]

Locale(Locale.getDefault()): zh_CN


Source: Tips4allCCNA FINAL EXAM

Comments

  1. It's a time zone change on December 31st in Shanghai.

    See this page for details of 1927 in Shanghai. Basically at midnight at the end of 1927, the clocks went back 5 minutes and 52 seconds. So "1927-12-31 23:54:08" actually happened twice, and it looks like Java is parsing it as the later possible instant for that local date/time - hence the difference.

    Just another episode in the often weird and wonderful world of time zones.

    ReplyDelete
  2. You've encountered a local time discontinuity:


    When local standard time was about to reach Sunday, 1. January 1928,
    00:00:00 clocks were turned backward 0:05:52 hours to Saturday, 31.
    December 1927, 23:54:08 local standard time instead


    This is not particularly strange and has happened pretty much everywhere at one time or another as timezones were switched or changed due to political or administrative actions.

    ReplyDelete
  3. Ran your code, output:

    -1325466353
    -1325466352
    1


    And:

    -1325466352
    -1325466351
    1


    on:

    $ java -version
    java version "1.6.0_20"
    OpenJDK Runtime Environment (IcedTea6 1.9.7) (fedora-52.1.9.7.fc14-i386)
    OpenJDK Client VM (build 19.0-b09, mixed mode)

    ReplyDelete
  4. The moral of this strangeness is:


    Use dates and times in UTC wherever possible.
    If you can not display a date or time in UTC, always indicate the time-zone.
    If you can not require an input date/time in UTC, require an explicitly indicated time-zone.

    ReplyDelete
  5. When incrementing time you should convert back to UTC and then add or subtract. Use the local time only for display.

    This way you will be able to walk through any periods where hours or minutes happen twice.

    If you converted to UTC, add each second, and convert to local time for display. You would go through 11:54:08 p.m. LMT - 11:59:59 p.m. LMT and then 11:54:08 p.m. CST - 11:59:59 p.m. CST.

    ReplyDelete
  6. Instead of converting each date in long you use the following code

    long i = (sDt4.getTime() - sDt3.getTime()) / (1000);
    System.out.println(i);


    And see the result is:

    1

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex