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 日以来的毫秒数


基础教程推荐
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 大摇大摆的枚举 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 从 python 访问 JVM 2022-01-01