简体   繁体   English

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

[英]Java.util.Calendar - milliseconds since Jan 1, 1970

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毫秒的时间有什么问题。数字小于10,000,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 长值:10000000

Calendar time in Millis: 10000000 Millis的日历时间:10000000

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

Long value: 1000000000000 长值:1000000000000

Calendar time in Millis: 1000000000000 Millis的日历时间:1000000000000

Date in YYYY-MM-DD format: 2001-8-8 YYYY-MM-DD格式的日期: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. 您从Calendar打印的日期是您的时区的本地日期,而时期定义为1970-01-01午夜的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. 因此,如果您居住在UTC西部的时区,那么您的日期将显示为1969-12-31,即使(在UTC中)它仍然是1970-01-01。

First, c.get(Calendar.MONTH) returns 0 for Jan, 1 for Feb, etc. 首先,c.get(Calendar.MONTH)为Jan返回0,为Feb返回1等。

Second, use DateFormat to output dates. 其次,使用DateFormat输出日期。

Third, your problems are a great example of how awkward Java's Date API is. 第三,你的问题是Java的Date API有多尴尬的一个很好的例子。 Use Joda Time API if you can. 如果可以,请使用Joda Time API。 It will make your life somewhat easier. 它会让你的生活更轻松。

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

Calendar#setTimeInMillis() sets the calendar's time to the number of milliseconds after Jan 1, 1970 GMT . Calendar#setTimeInMillis()将日历的时间设置为GMT 1970年1月1日之后的毫秒数。

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

遗憾的是, java.util.Datejava.util.Calendar的设计很糟糕,导致了这种混乱。

您的时区很可能落后于GMT(例如,GMT-5),因此您的时区中的10,000,000毫秒来自1969年12月31日,但由于java.util.Calendar中的月份为零,因此您的日历 - >文本转换存在缺陷你得到1969-11-31而不是预期的1969-12-31。

You can figure out yourself if you change your first c.setTimeInMillis(l); 如果你改变你的第一个c.setTimeInMillis(l);你可以弄清楚自己c.setTimeInMillis(l); in c.clear(); c.clear();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM