Java日期(0)不是1970年1月1日 [英] Java Date(0) is not 1/1/1970

查看:80
本文介绍了Java日期(0)不是1970年1月1日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望它会打印 1970-01-01-00:00:00 ,但它会打印 1970-12-31-19:00:00

I was expecting this to print 1970-01-01-00:00:00, but it prints 1970-12-31-19:00:00

我对日期的计算方式有何误解?已经过了一年了.我正在Windows 7和JDK 1.6上运行它

What am I misinterpreting about how Date is counted from? It is one year off. I am running this on Windows 7 with JDK 1.6

System.out.println(new SimpleDateFormat("YYYY-MM-dd-HH:mm:ss").format(new Date(0)));

推荐答案

不仅仅是时区-它是使用时区使用 YYYY 的混合表示 year year 而不是 year (从Java 7开始).将其更改为 yyyy ,您将看到1969.

It's not just the time zone - it's a mixture of using the time zone and using YYYY which indicates the week year rather than the year (as of Java 7). Change it to yyyy and you'll see 1969.

每周的文档状态:

为WEEK_OF_YEAR字段计算的值范围为1到53.日历年的第一周是最早的7天时段,从getFirstDayOfWeek()开始,该日期至少包含该年的getMinimalDaysInFirstWeek()天.因此,它取决于getMinimalDaysInFirstWeek(),getFirstDayOfWeek()和1月1日的星期几的值.从一年的第1周到下一年的第1周(不包括)之间的周从2到52依次编号,或者53(不涉及朱利安-格里高利安过渡的年份).

Values calculated for the WEEK_OF_YEAR field range from 1 to 53. The first week of a calendar year is the earliest seven day period starting on getFirstDayOfWeek() that contains at least getMinimalDaysInFirstWeek() days from that year. It thus depends on the values of getMinimalDaysInFirstWeek(), getFirstDayOfWeek(), and the day of the week of January 1. Weeks between week 1 of one year and week 1 of the following year (exclusive) are numbered sequentially from 2 to 52 or 53 (except for year(s) involved in the Julian-Gregorian transition).

在构造GregorianCalendar时,使用依赖于语言环境的资源来初始化getFirstDayOfWeek()和getMinimalDaysInFirstWeek()值.当getFirstDayOfWeek()为MONDAY且getMinimalDaysInFirstWeek()为4时,星期确定与ISO 8601标准兼容,该值在首选该标准的语言环境中使用.可以通过调用setFirstDayOfWeek()和setMinimalDaysInFirstWeek()显式设置这些值.

The getFirstDayOfWeek() and getMinimalDaysInFirstWeek() values are initialized using locale-dependent resources when constructing a GregorianCalendar. The week determination is compatible with the ISO 8601 standard when getFirstDayOfWeek() is MONDAY and getMinimalDaysInFirstWeek() is 4, which values are used in locales where the standard is preferred. These values can explicitly be set by calling setFirstDayOfWeek() and setMinimalDaysInFirstWeek().

一周的一年与WEEK_OF_YEAR周期保持同步.第一个星期和最后一个星期(包括最后一个星期)之间的所有星期都具有相同的星期年份值.因此,一周的第一天和最后一天可能具有不同的日历年值.

A week year is in sync with a WEEK_OF_YEAR cycle. All weeks between the first and last weeks (inclusive) have the same week year value. Therefore, the first and last days of a week year may have different calendar year values.

因此问题是正常年份"的最后一天会出现.1969年实际上是周年"1970年.

So the problem is that the last day of "normal year" 1969 is actually in the "week year" of 1970.

您应该绝对使用 yyyy 以避免混淆-一周的使用相对很少,并且很容易引起混淆,如我们所见:)

You should definitely use yyyy to avoid confusion - week years are relatively rarely useful, and can easily cause confusion, as we've seen :)

如评论中所述,您确定不使用Java 7吗?1.6中 SimpleDateFormat 文档不用说了……也许您正在1.6中进行编译,而正在运行在7中进行编译?

As noted in comments, are you sure you're not using Java 7? The docs for SimpleDateFormat in 1.6 don't mention it... perhaps you're compiling in 1.6 but running in 7?

这篇关于Java日期(0)不是1970年1月1日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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