问题:
If I run the following program, which parses two date strings referencing times 1 second apart and compares them: 如果我运行以下程序,该程序将解析两个日期字符串,它们分别引用间隔为1秒的时间并进行比较:
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(ld4-ld3);
}
The output is: 输出为:
353 353
Why is ld4-ld3
not 1
(as I would expect from the one-second difference in the times), but 353
? 为什么ld4-ld3
不是1
(就像我从一秒的时间差中期望的那样),而是353
?
If I change the dates to times 1 second later: 如果我将日期更改为1秒后的时间:
String str3 = "1927-12-31 23:54:08";
String str4 = "1927-12-31 23:54:09";
Then ld4-ld3
will be 1
. 然后ld4-ld3
将为1
。
Java version: Java版本:
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
解决方案:
参考一: https://stackoom.com/question/Shk5/为什么将这两次相减-在-年-会得出奇怪的结果参考二: https://oldbug.net/q/Shk5/Why-is-subtracting-these-two-times-in-1927-giving-a-strange-result
来源:oschina
链接:https://my.oschina.net/stackoom/blog/4448918