Java Gregorian Calendar Returns Wrong Month(Java 公历返回错误的月份)
问题描述
所以我现在在这里待了几个小时,它返回了正确的年份和日期,但由于某些奇怪的原因,它返回了错误的月份.我确定这是一个简单的解决方法,但我似乎无法弄清楚.
tl;dr
要获取当前月份的数字 1-12:
最好指定您想要/预期的时区.
类似调用.getYear()
和.getDayOfMonth()
.
详情
<块引用>返回错误的月份
正如其他人所说,在 Calendar
中,1 月至 12 月的月份疯狂地编号为 0-11,而不是 1-12.旧日期时间类中许多糟糕的设计决策之一.这些类现在是遗留的,被 java.time 类所取代.
那么有办法解决这个问题吗?
是的,有一个解决方法.使用一个好的日期时间库而不是 java.util.Date/Calendar 的混乱.现代方式是使用 java.time 类.
当前时刻
时区对于获取当前日期和时间至关重要.对于任何给定的时刻,日期和挂钟时间因地区而异.
您可以通过 Month
枚举和日期.
<块引用>
当前:2016-12-14T04:54:44.802-05:00[美国/蒙特利尔]
2016 年
月份是 12
月份名称是十二月
第 14 天
查看 IdeOne.com 中的实时代码.
如果您只关心日期而不关心时间,请使用 LocalDate
类.
特定时刻
您可以将时刻指定为自 epoch 以来的毫秒数1970 年 UTC 的第一刻.
<块引用>
instant.toString(): 2009-02-13T23:31:38.765Z
该输出中的 Z
是 Zulu
的缩写,意思是 UTC.
您可以指定时区以调整到特定的挂钟时间.
<块引用>
zdt.toString(): 2009-02-13T18:31:38.765-05:00[美国/蒙特利尔]
查看IdeOne.com 中的实时代码.
我确实不建议以这种方式交换日期时间数据.最好序列化为 ISO 8601 格式的文本.例如:2009-02-13T23:31:38.765Z
关于java.time
java.time 框架内置于 Java 8 及更高版本.这些类取代了麻烦的旧 legacy 日期时间类,例如 java.util.Date
, 日历
, &SimpleDateFormat
.
Joda-Time 项目,现在在 维护模式,建议迁移到 java.time 类.
要了解更多信息,请参阅 Oracle 教程.并在 Stack Overflow 上搜索许多示例和解释.规范是 JSR 310.
从哪里获得 java.time 类?
- Java SE 8 和 SE 9 及更高版本
- 内置.
- 标准 Java API 的一部分,带有捆绑实现.
- Java 9 添加了一些小功能和修复.
- Java SE 6 和 SE 7
- 大部分 java.time 功能都向后移植到 Java 6 &7 在 ThreeTen-Backport.
- Android
- ThreeTenABP 项目采用 ThreeTen-专门针对 Android 的反向移植(如上所述).
- 请参阅如何使用…….
ThreeTen-Extra 项目通过附加类扩展了 java.time.该项目是未来可能添加到 java.time 的试验场.您可以在这里找到一些有用的类,例如 间隔
,YearWeek
, YearQuarter
和 更多.
旧答案 - Joda-Time
更新:Joda-Time 项目,现在在 维护模式,建议迁移到 java.time 类.
- 现在使用 Joda-Time 2.3.
- 未来,在 Java 8 中,考虑迁移到 JSR 310:日期和时间API 取代了 Date/Calendar 类,并受到 Joda-Time 的启发.
示例代码
今天
运行时……
有一天
运行时……
<小时>
附:当我注意到你随意选择的龙导致了十三号星期五时,我的背脊发凉!
So I been at this for a few hours now and it returns the correct Year, and Day but for some odd reason it returns the wrong month. I'm sure its a simple fix but I can't seem to figure it out.
tl;dr
To get a number 1-12 for current month:
Better to specify your desired/expected time zone.
Similarly call .getYear()
and .getDayOfMonth()
.
Details
it returns the wrong month
As others said, in Calendar
the months January-December are crazily numbered 0-11 rather than 1-12. One of many poor design decisions in the old date-time classes. Those classes are now legacy, supplanted by the java.time classes.
So is there a work around this?
Yes, there is a workaround. Use a good date-time library rather than the mess that is java.util.Date/Calendar. The modern way is with the java.time classes.
Current moment
Time zone is crucial in getting the current date and time. For any given moment the date and wall-clock time vary by zone.
You can interrogate for the various components such as year, month number, localized name of month via Month
enum, and day-of-month.
Current: 2016-12-14T04:54:44.802-05:00[America/Montreal]
Year is 2016
Month is 12
Month name is décembre
Day is 14
See live code in IdeOne.com.
If you only care about the date and not the time-of-day, use the LocalDate
class.
Specific moment
You can specify a moment as a count of milliseconds since the epoch of first moment of 1970 in UTC.
instant.toString(): 2009-02-13T23:31:38.765Z
The Z
in that output is short for Zulu
and means UTC.
You can assign a time zone to adjust into a particular wall-clock time.
zdt.toString(): 2009-02-13T18:31:38.765-05:00[America/Montreal]
See live code in IdeOne.com.
I do not recommend exchanging date-time data this way. Better to serialize to text in ISO 8601 formats. For example: 2009-02-13T23:31:38.765Z
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
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
- Java SE 8 and SE 9 and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
- See How to use….
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.
Old Answer - Joda-Time
Update: The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
- Use Joda-Time 2.3 now.
- In the future, with Java 8, consider moving to JSR 310: Date and Time API which supplants the Date/Calendar classes and is inspired by Joda-Time.
Example Code
Today
When run…
Some Day
When run…
P.S. I just got the chills down my back when I noticed your arbitrarily chosen Long resulted in Friday The Thirteenth!
这篇关于Java 公历返回错误的月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!