将光标移到/点击文章中的句子上,可以查看译文。      显示繁体中文内容    显示简体中文内容

Why is subtracting these two times (in 1927) giving a strange result?
为什么减去两个时间( 以1927 ) 会给出一个奇怪的结果?

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 one 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

时间:

It's a time zone change on December 31 st 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.

EDIT : Stop the press!History changes...

The original question would no longer demonstrate quite the same behaviour, if rebuilt with version 2013 a of TZDB.in 2013 a, the result would be 358 seconds, with a transition time of 23 :54:03 instead of 23 :54:08.

I only noticed this because i'm collecting questions like this in Noda Time, in the form of unit tests.. . the test has now been changed, but it just goes to show - not even historical data is safe.

EDIT : History has changed again...

In TZDB 2014 f, the time of the change has moved to 1900-12-31, and it's now a mere 343 second change (so the time between t and t+1 is 344 seconds, if you see what i mean).

EDIT : to answer Ken Kin's question around a transition at 1900... it looks like the Java timezone implementation treats all time zones as simply being in their standard time for any instant before the start of 1900 UTC :


import java.util.TimeZone;

public class Test {
 public static void main(String[] args) throws Exception {
 long startOf1900Utc = -2208988800000L;
 for (String id : TimeZone.getAvailableIDs()) {
 TimeZone zone = TimeZone.getTimeZone(id);
 if (zone.getRawOffset()!= zone.getOffset(startOf1900Utc - 1)) {
 System.out.println(id);
 }
 }
 }
}

The code above produces no output on my Windows machine.so any time zone which has any offset other than its standard one at the start of 1900 will count that as a transition.TZDB itself has some data going back earlier than that, and doesn't rely on any idea of a"fixed"standard time (which is what getRawOffset assumes to be a valid concept) so other libraries needn't introduce this artificial transition.

I ran your code, the output is :


-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)

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.

Instead of converting each date, you use the following code


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

And see the result is :

 
1

 

I'm sorry to say that, but the time discontinuity has moved a bit in

JDK 6 two years ago, and in JDK 7 just recently in update 25.

Lesson to learn : avoid non-UTC times at all costs, except, maybe, for display.

As explained by others, there's a time discontinuity there.there are two possible timezone offsets for 1927-12-31 23:54:08 at Asia/Shanghai, but only one offset for 1927-12-31 23:54:07.So, depending on which offset is used, there's either a one second difference or a 5 minutes and 53 seconds difference.

This slight shift of offsets, instead of the usual one-hour daylight savings (summer time) we are used to, obscures the problem a bit.

Note that the 2013 a update of the timezone database moved this discontinuity a few seconds earlier, but the effect would still be observable.

The new java.time package on Java 8 let use see this more clearly, and provide tools to handle it.Given :


DateTimeFormatterBuilder dtfb = new DateTimeFormatterBuilder();
dtfb.append(DateTimeFormatter.ISO_LOCAL_DATE);
dtfb.appendLiteral(' ');
dtfb.append(DateTimeFormatter.ISO_LOCAL_TIME);
DateTimeFormatter dtf = dtfb.toFormatter();
ZoneId shanghai = ZoneId.of("Asia/Shanghai");

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

ZonedDateTime zdt3 = LocalDateTime.parse(str3, dtf).atZone(shanghai);
ZonedDateTime zdt4 = LocalDateTime.parse(str4, dtf).atZone(shanghai);

Duration durationAtEarlierOffset = Duration.between(zdt3.withEarlierOffsetAtOverlap(), zdt4.withEarlierOffsetAtOverlap());

Duration durationAtLaterOffset = Duration.between(zdt3.withLaterOffsetAtOverlap(), zdt4.withLaterOffsetAtOverlap());

Then durationAtEarlierOffset will be one second, while durationAtLaterOffset will be five minutes and 53 seconds.

Also, these two offsets are the same :


//Both have offsets +08:05:52
ZoneOffset zo3Earlier = zdt3.withEarlierOffsetAtOverlap().getOffset();
ZoneOffset zo3Later = zdt3.withLaterOffsetAtOverlap().getOffset();

But these two are different :


//+08:05:52
ZoneOffset zo4Earlier = zdt4.withEarlierOffsetAtOverlap().getOffset();

//+08:00
ZoneOffset zo4Later = zdt4.withLaterOffsetAtOverlap().getOffset();

You can see the same problem comparing 1927-12-31 23:59:59 with 1928-01-01 00:00:00, though, in this case, it is the earlier offset that produce the longer divergence, and it is the earlier date that has two possible offsets.

Another way to approach this is to check whether there's a transition going on.we can do this like this :


//Null
ZoneOffsetTransition zot3 = shanghai.getRules().getTransition(ld3.toLocalDateTime);

//An overlap transition
ZoneOffsetTransition zot4 = shanghai.getRules().getTransition(ld3.toLocalDateTime);

You can check whether the transition is an overlap - in which case there's more than one valid offset for that date/time - or a gap - in which case that date/time is not valid for that zone id - by using the isOverlap() and isGap() methods on zot4.

I hope this helps people handle this sort of issue once Java 8 becomes widely available, or to those using Java 7 who adopt the JSR 310 backport.

...