Java.util.Calendar - 1970年1月1日以来的毫秒数 [英] Java.util.Calendar - milliseconds since Jan 1, 1970

查看:548
本文介绍了Java.util.Calendar - 1970年1月1日以来的毫秒数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程序后跟输出。有人请向我解释为什么从1970年1月1日开始的10,000,000毫秒是1969年11月31日。好的,有人请解释我的假设,即第一次测试应该从1970年1月1日起产生一个时间10,000,000毫秒的错误。小于10,000,000的数字产生同样的结果。

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("\nLong 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);
}




长值:10000000

Long value: 10000000

Millis的日历时间:10000000

Calendar time in Millis: 10000000

以YYYY-MM-DD格式显示的日期:1969-11-31

Date in YYYY-MM-DD format: 1969-11-31

长整型值:1000000000000

Long value: 1000000000000

Millis中的日历时间:1000000000000

Calendar time in Millis: 1000000000000

以YYYY-MM-DD格式的日期:2001-8-8

Date in YYYY-MM-DD format: 2001-8-8


推荐答案

打印从日历是您的时区的本地,而时代定义为1970-01-01在UTC的午夜。所以如果你居住在UTC的西部时区,那么你的日期将显示为1969-12-31,即使(UTC)它仍然是1970-01-01。

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日以来的毫秒数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆