Java.util.Calendar - milliseconds since Jan 1, 1970(Java.util.Calendar - 自 1970 年 1 月 1 日以来的毫秒数)
问题描述
程序后跟输出.有人请向我解释为什么从 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 日以来的毫秒数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java.util.Calendar - 自 1970 年 1 月 1 日以来的毫秒数


基础教程推荐
- 在螺旋中写一个字符串 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01