問題描述
程序后跟輸出.有人請向我解釋為什么從 1970 年 1 月 1 日開始的 10,000,000 毫秒是 1969 年 11 月 31 日.好吧,請有人解釋我的假設有什么問題,即第一次測試應該從 1970 年 1 月 1 日開始產生 10,000,000 毫秒的時間.小于 10,000,000 的數字產生同樣的結果.
public static void main(String[] args) {字符串 x = "10000000";長 l = new Long(x).longValue();System.out.println("長值:" + l);日歷 c = new GregorianCalendar();c.setTimeInMillis(l);System.out.println("以米為單位的日歷時間:" + c.getTimeInMillis());字符串 origDate = c.get(Calendar.YEAR) + "-" + c.get(Calendar.MONTH) + "-" + c.get(Calendar.DAY_OF_MONTH);System.out.println("YYYY-MM-DD 格式的日期:" + origDate);x = "1000000000000";l = new Long(x).longValue();System.out.println("
長值:" + l);c.setTimeInMillis(l);System.out.println("以米為單位的日歷時間:" + c.getTimeInMillis());origDate = c.get(Calendar.YEAR) + "-" + c.get(Calendar.MONTH) + "-" + c.get(Calendar.DAY_OF_MONTH);System.out.println("YYYY-MM-DD 格式的日期:" + origDate);}
<塊引用>
長值:10000000
米利斯的日歷時間:10000000
YYYY-MM-DD 格式的日期:1969-11-31
長值:1000000000000
米利斯的日歷時間:1000000000000
YYYY-MM-DD 格式的日期:2001-8-8
您從 Calendar
打印的日期是您所在時區的本地日期,而紀元定義為 1970-01-01 的午夜在UTC.因此,如果您居住在 UTC 以西的時區,那么您的日期將顯示為 1969-12-31,即使(在 UTC 中)它仍然是 1970-01-01.
Program followed by output. Someone please explain to me why 10,000,000 milliseconds from Jan 1, 1970 is November 31, 1969. Well, someone please explain what's wrong with my assumption that the first test should produce a time 10,000,000 milliseconds from Jan 1, 1970. Numbers smaller than 10,000,000 produce the same result.
public static void main(String[] args) {
String x = "10000000";
long l = new Long(x).longValue();
System.out.println("Long value: " + l);
Calendar c = new GregorianCalendar();
c.setTimeInMillis(l);
System.out.println("Calendar time in Millis: " + c.getTimeInMillis());
String origDate = c.get(Calendar.YEAR) + "-" + c.get(Calendar.MONTH) + "-" + c.get(Calendar.DAY_OF_MONTH);
System.out.println("Date in YYYY-MM-DD format: " + origDate);
x = "1000000000000";
l = new Long(x).longValue();
System.out.println("
Long value: " + l);
c.setTimeInMillis(l);
System.out.println("Calendar time in Millis: " + c.getTimeInMillis());
origDate = c.get(Calendar.YEAR) + "-" + c.get(Calendar.MONTH) + "-" + c.get(Calendar.DAY_OF_MONTH);
System.out.println("Date in YYYY-MM-DD format: " + origDate);
}
Long value: 10000000
Calendar time in Millis: 10000000
Date in YYYY-MM-DD format: 1969-11-31
Long value: 1000000000000
Calendar time in Millis: 1000000000000
Date in YYYY-MM-DD format: 2001-8-8
The dates you print from Calendar
are local to your timezone, whereas the epoch is defined to be midnight of 1970-01-01 in UTC. So if you live in a timezone west of UTC, then your date will show up as 1969-12-31, even though (in UTC) it's still 1970-01-01.
這篇關于Java.util.Calendar - 自 1970 年 1 月 1 日以來的毫秒數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!