Date() 对象不一致。月、日、年都错了
现在是 2011 年 3 月 15 日,当我调用新的日期对象时:
Date now = new Date();
我返回的
- 月份为 2 (
getMonth()
), - 日期为 2 (
getDay()
) - 和年份 (
getYear()
) 为 111。
这种约定有什么原因吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
直接来自类的文档:
至于
getDay ()
:2011 年 3 月 15 日实际上是星期二。
Straight from the class's documentation:
And as for
getDay()
:March 15th 2011 is in fact a Tuesday.
原因是它是 Date 的 javadoc 所指定的;请参阅@matt b 的回答。
Date
API 是在 JDK 1.0 时代创建的,众所周知,它在许多领域都存在问题。这就是为什么大多数 Date 方法被标记为“已弃用”的原因。 (顺便说一句,这意味着建议您不要在新代码中使用它们!)Calendar
API 是对Date
的重大改进,但是迄今为止,在 Java 中处理日期/时间值的最佳 API 是第 3 方 Joda time API。如果您想要 Joda 时间使用的示例,请查看上面的链接。
GregorianCalendar< 中有一个日历使用示例/code>javadocs
。有关日历使用的更多示例,请参见此页面。
The reason is that it is what the javadoc for Date specifies; see @matt b's answer.
The
Date
APIs were created in the days of JDK 1.0, and are well known to be problematic in a number of areas. That is why most of the Date methods are marked as Deprecated. (By the way, that means that it is recommended that you don't use them in new code!!)The
Calendar
APIs are a significant improvement onDate
, but the best by far APIs for handling date / time values in Java are the 3rd-party Joda time APIs.If you want examples of Joda time usage, look at the link above. There's an example of Calendar usage in the
GregorianCalendar
javadocs. More examples of Calendar usage may be found on this page.tl;dr
…and:
…and:
详细信息
显然,您正在使用两个可怕的日期时间类中的任何一个:
java.util.Date
或java.sql.Date
。随着 JSR 310 的采用,它们都已经过时了,定义了它们的替代品,即现代的 java.time 类。LocalDate
LocalDate
类表示仅日期值,没有时间且没有 时区 或 相对 UTC 的偏移量 。时区对于确定日期至关重要。对于任何特定时刻,全球各地的日期都会因地区而异。例如,在法国巴黎午夜过后几分钟,又是新的一天“昨天”在魁北克蒙特利尔。
如果未指定时区,JVM 会隐式应用其当前的默认时区。该默认值可能在运行时随时更改(!),因此您的结果可能会有所不同。最好明确指定您想要/预期的时区作为参数。如果重要,请与您的用户确认该区域。
以
大陆/地区
格式指定正确的时区名称 ,例如美国/蒙特利尔
、非洲/卡萨布兰卡
或太平洋/奥克兰
。切勿使用 2-4 个字母的缩写,例如EST
或IST
,因为它们不是真正的时区,不是标准化的,甚至不是唯一的( !)。如果您想使用 JVM 当前的默认时区,请询问它并作为参数传递。如果省略,代码读起来会变得不明确,因为我们不确定您是否打算使用默认值,或者您是否像许多程序员一样没有意识到这个问题。
或者指定一个日期。您可以用数字设置月份,1 月至 12 月的合理编号为 1-12。
或者,更好的是,使用
Month
预定义的枚举对象,一个代表一年中的每个月。提示:在整个代码库中使用这些Month
对象,而不仅仅是整数,可以使您的代码更加自记录,确保有效值,并提供 类型安全。年份
&YearMonth
。访问日期的部分
java.time 类使用合理的编号,1-12 表示月份,1-7 表示星期几,年份编号(例如 2019 年)是 2019 年,等等。
关于 java.time
java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧遗留日期时间类,例如
java.util.Date
,日历
, & ;SimpleDateFormat
。要了解更多信息,请参阅 Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范为 JSR 310。
Joda-Time 项目,现已在 维护模式,建议迁移到 java.time 类。
您可以直接与数据库交换java.time对象。使用符合 JDBC 驱动程序 /jeps/170" rel="nofollow noreferrer">JDBC 4.2 或更高版本。不需要字符串,不需要 java.sql.* 类。
从哪里获取 java.time 类?
ThreeTen-Extra 项目通过附加类扩展了 java.time 。该项目是 java.time 未来可能添加的内容的试验场。您可能会在这里找到一些有用的类,例如
间隔
,YearWeek
,<代码>YearQuarter,以及更多 。tl;dr
…and:
…and:
Details
Apparently you are using either of two terrible date-time classes,
java.util.Date
orjava.sql.Date
. Both are outmoded as of the adoption of JSR 310, defining their replacement, the modern java.time classes.LocalDate
The
LocalDate
class represents a date-only value without time-of-day and without time zone or offset-from-UTC.A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment during runtime(!), so your results may vary. Better to specify your desired/expected time zone explicitly as an argument. If critical, confirm the zone with your user.
Specify a proper time zone name in the format of
Continent/Region
, such asAmerica/Montreal
,Africa/Casablanca
, orPacific/Auckland
. Never use the 2-4 letter abbreviation such asEST
orIST
as they are not true time zones, not standardized, and not even unique(!).If you want to use the JVM’s current default time zone, ask for it and pass as an argument. If omitted, the code becomes ambiguous to read in that we do not know for certain if you intended to use the default or if you, like so many programmers, were unaware of the issue.
Or specify a date. You may set the month by a number, with sane numbering 1-12 for January-December.
Or, better, use the
Month
enum objects pre-defined, one for each month of the year. Tip: Use theseMonth
objects throughout your codebase rather than a mere integer number to make your code more self-documenting, ensure valid values, and provide type-safety. Ditto forYear
&YearMonth
.Accessing parts of a date
The java.time classes use sane numbering, 1-12 for months, 1-7 for days of the week, the year number such as 2019 is the year 2019, and such.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
,Calendar
, &SimpleDateFormat
.To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for
java.sql.*
classes.Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as
Interval
,YearWeek
,YearQuarter
, and more.