程序后跟输出.有人请向我解释为什么从 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模板网!
解析 ISO 8601 字符串本地日期时间,就像在 UTC 中Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期时间,就像在 UTC 中一样)
如何将公历字符串转换为公历?How to convert Gregorian string to Gregorian Calendar?(如何将公历字符串转换为公历?)
Java:GregorianCalendar 的最大值和最小值是什么/在哪Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
1582 年 10 月 15 日之前日期的日历到日期转换.公历Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日历到日期转换
java日历setFirstDayOfWeek不起作用java Calendar setFirstDayOfWeek not working(java日历setFirstDayOfWeek不起作用)
Java:获取当前星期几的值Java: getting current Day of the Week value(Java:获取当前星期几的值)