Java.util.Calendar - 自 1970 年 1 月 1 日以来的毫秒数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/263376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:12:05  来源:igfitidea点击:

Java.util.Calendar - milliseconds since Jan 1, 1970

javacalendar

提问by Nathan Spears

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.

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

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);
}

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

多头值:10000000

以米为单位的日历时间:10000000

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

多头值:1000000000000

以米为单位的日历时间:1000000000000

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

采纳答案by Chris Jester-Young

The dates you print from Calendarare 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.

您打印的日期Calendar是您所在时区的本地日期,而纪元被定义为 UTC 中的 1970-01-01 午夜。因此,如果您居住在 UTC 以西的时区,那么您的日期将显示为 1969-12-31,即使(在 UTC 中)它仍然是 1970-01-01。

回答by Davide

You can figure out yourself if you change your first c.setTimeInMillis(l);in c.clear();

如果你改变你的第一个c.setTimeInMillis(l);,你可以弄清楚自己c.clear();

回答by Steve McLeod

First, c.get(Calendar.MONTH)returns 0 for Jan, 1 for Feb, etc.

首先,c.get(Calendar.MONTH)1 月返回 0,2 月返回 1,依此类推。

Second, use DateFormatto output dates.

其次,用于DateFormat输出日期。

Third, your problems are a great example of how awkward Java's Date API is. Use Joda Time API if you can. It will make your life somewhat easier.

第三,您的问题是 Java 的 Date API 多么笨拙的一个很好的例子。如果可以,请使用 Joda Time API。它会让你的生活更轻松一些。

Here's a better example of your code, which indicates the timezone:

这是您的代码的一个更好的示例,它指示时区:

public static void main(String[] args) {

    final DateFormat dateFormat = SimpleDateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);

    long l = 10000000L;
    System.out.println("Long value: " + l);
    Calendar c = new GregorianCalendar();
    c.setTimeInMillis(l);
    System.out.println("Date: " + dateFormat.format(c.getTime()));

    l = 1000000000000L;
    System.out.println("\nLong value: " + l);
    c.setTimeInMillis(l);
    System.out.println("Date: " + dateFormat.format(c.getTime()));
}

回答by Mike

Calendar#setTimeInMillis()sets the calendar's time to the number of milliseconds after Jan 1, 1970 GMT.

Calendar#setTimeInMillis()将日历的时间设置为 1970 年 1 月 1 日GMT之后的毫秒数。

Calendar#get()returns the requested field adjusted for the calendar's timezone which, by default, is your machine's local timezone.

Calendar#get()返回针对日历时区调整的请求字段,默认情况下,它是您机器的本地时区

This should work as you expect if you specify "GMT" timezone when you construct the calendar:

如果您在构造日历时指定“GMT”时区,这应该可以正常工作:

Calendar c = new GregorianCalendar(TimeZone.getTimeZone("GMT"));

回答by Alexander

Your timezone is most likely lagging behind GMT (e.g., GMT-5), therefore 10,000,000ms from epoch is December 31 1969 in your timezone, but since months are zero-based in java.util.Calendaryour Calendar-to-text conversion is flawed and you get 1969-11-31 instead of the expected 1969-12-31.

你的时区是最有可能落后于格林尼治标准时间(如GMT-5),因此从纪元10,000,000ms是1969年12月31日在您的时区,但因为月是零基础的java.util.CalendarCalendar-to-文本转换是有缺陷的,你会得到1969 11-31 而不是预期的 1969-12-31。

回答by Julien Chastang

Sadly, java.util.Dateand java.util.Calendarwere poorly designed leading to this sort of confusion.

遗憾的是,java.util.Datejava.util.Calendar进行了设计不当导致了这种混乱。