日历返回错误的月份
Calendar rightNow = Calendar.getInstance();
String month = String.valueOf(rightNow.get(Calendar.MONTH));
执行上述代码片段后,月份的值为 10,而不是 11。这是怎么回事?
Calendar rightNow = Calendar.getInstance();
String month = String.valueOf(rightNow.get(Calendar.MONTH));
After the execution of the above snippet, month gets a value of 10 instead of 11. How come?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
月份从 0 开始索引,而不是从 1 开始,因此 10 是 11 月,11 是 12 月。
Months are indexed from 0 not 1 so 10 is November and 11 will be December.
它们从 0 开始 - 检查 文档< /a>
They start from 0 - check the docs
许多答案都清楚地表明:月份从 0 开始。
这里有一个提示:您应该使用 SimpleDateFormat 来获取月份的字符串表示形式:
输出:
注意:输出可能会有所不同,它是特定于区域设置的。
As is clear by the many answers: the month starts with 0.
Here's a tip: you should be using SimpleDateFormat to get the String-representation of the month:
Output:
Note: the output may vary, it is Locale-specific.
正如几个人指出的那样, Calendar< 返回的月份Java 中的 /a> 和 Date 类是从 0 而不是 1 进行索引。因此 0 是 1 月,当前月份(11 月)是 10。
您可能想知道为什么会出现这种情况。起源于 POSIX 标准函数
ctime
、gmtime
和localtime
,它们接受或返回一个time_t
结构以下字段(来自 man 3 ctime):此 API 几乎完全复制到Java 1.0 中的 Java Date 类,以及从那里大部分原封不动地进入 Java 1.1 中的 Calendar 类。 Sun 在引入 Calendar 时解决了最明显的问题,即公历 2001 年在 Date 类中由值 101 表示。但我不确定他们为什么不更改日期和月份值,使其至少在索引中保持一致,无论是从零还是从一。这种不一致和相关的混乱至今仍然存在于 Java(和 C)中。
As several people have pointed out, months returned by the Calendar and Date classes in Java are indexed from 0 instead of 1. So 0 is January, and the current month, November, is 10.
You might wonder why this is the case. The origins lie with the POSIX standard functions
ctime
,gmtime
andlocaltime
, which accept or return atime_t
structure with the following fields (from man 3 ctime):This API was copied pretty much exactly into the Java Date class in Java 1.0, and from there mostly intact into the Calendar class in Java 1.1. Sun fixed the most glaring problem when they introduced Calendar – the fact that the year 2001 in the Gregorian calendar was represented by the value 101 in their Date class. But I'm not sure why they didn't change the day and month values to at least both be consistent in their indexing, either from zero or one. This inconsistency and related confusion still exists in Java (and C) to this day.
月份从零开始,就像列表的索引一样。
因此,一月 = 0,二月 = 1,依此类推。
Months start from zero, like indexes for lists.
Therefore Jan = 0, Feb = 1, etc.
从API:
http://java.sun.com/j2se /1.5.0/docs/api/java/util/Calendar.html
From the API:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html
tl;dr
详细信息
其他答案是正确的但已过时。
麻烦的旧日期时间类有许多糟糕的设计选择和缺陷。其中之一是从零开始计数月份数字 0-11,而不是明显的 1-12。
java.time
java.time 框架内置于 Java 8 及更高版本中。这些类取代了旧的麻烦的日期时间类,例如 java.util.Date、.Calendar 和 & 。
java.text.SimpleDateFormat
。现在处于维护模式,Joda-Time 项目还建议迁移到 java.time。
要了解更多信息,请参阅 Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。
许多 java.time 功能都向后移植到 Java 6 和 Java 6。 ThreeTen-Backport 中的 7 并在 ThreeTenABP。
ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是 java.time 未来可能添加的内容的试验场。
月份 1-12
在 java.time 中,月份数字确实是 1-12 月的预期 1-12。
LocalDate
类表示仅日期值,没有时间和时区。时区
时区对于确定日期至关重要。对于任何特定时刻,全球各地的日期都会因地区而异。例如,在法国巴黎午夜过后几分钟,又是新的一天“昨天”在魁北克蒙特利尔。
以
大陆/地区
格式指定正确的时区名称 ,例如America/Montreal
、非洲/卡萨布兰卡
,或太平洋/奥克兰
。切勿使用 3-4 个字母的缩写,例如EST
或IST
,因为它们不是真正的时区,不是标准化的,甚至不是唯一的( !)。如果您想要时区的日期时间,请以相同的方式使用
ZonedDateTime
对象。转换遗留类
如果您有
GregorianCalendar< /code>
现有对象,使用新的
toZonedDateTime
方法添加到旧类中。有关更多转换信息,请参阅将 java.util.Date 转换为什么“java.time”类型? a>Month
枚举java.time 类包括方便的
月
枚举。在代码中使用此类的实例而不是单纯的整数,可以使代码更加自文档化,提供 type-安全,并确保有效值。Month
枚举提供了有用的方法,例如使用 月份的本地化名称。关于 java.time
java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧遗留日期时间类,例如
java.util.Date
,日历
, & ;SimpleDateFormat
。Joda-Time 项目,现已在 维护模式,建议迁移到 java.time 类。
要了解更多信息,请参阅 Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范为 JSR 310。
从哪里获取 java.time 类?
ThreeTen-Extra 项目通过附加类扩展了 java.time 。该项目是 java.time 未来可能添加的内容的试验场。您可能会在这里找到一些有用的类,例如
间隔
,YearWeek
,<代码>YearQuarter,以及更多 。tl;dr
Details
Other answers are correct but outdated.
The troublesome old date-time classes had many poor design choices and flaws. One was the zero-based counting of month numbers 0-11 rather than the obvious 1-12.
java.time
The java.time framework is built into Java 8 and later. These classes supplant the old troublesome date-time classes such as
java.util.Date
,.Calendar
, &java.text.SimpleDateFormat
.Now in maintenance mode, the Joda-Time project also advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time.
Months 1-12
In java.time the month number is indeed the expected 1-12 for January-December.
The
LocalDate
class represents a date-only value without time-of-day and without time zone.Time zone
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.
Specify a proper time zone name in the format of
continent/region
, such asAmerica/Montreal
,Africa/Casablanca
, orPacific/Auckland
. Never use the 3-4 letter abbreviation such asEST
orIST
as they are not true time zones, not standardized, and not even unique(!).If you want a date-time for a time zone, use
ZonedDateTime
object in the same way.Convert legacy classes
If you have a
GregorianCalendar
object in hand, convert toZonedDateTime
using newtoZonedDateTime
method added to the old class. For more conversion info, see Convert java.util.Date to what “java.time” type?Month
enumThe java.time classes include the handy
Month
enum, by the way. Use instances of this class in your code rather than mere integers to make your code more self-documenting, provide type-safety, and ensure valid values.The
Month
enum offers useful methods such as generating a String with the localized name of the month.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?
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.上面的语句给出了月份的确切数字。由于
get(Calendar.Month)
返回从 0 开始的月份,因此在结果中添加 1 将给出正确的输出。设置月份时请记住减去 1。或者使用提供的常量变量。
The above statement gives the exact number of the month. As
get(Calendar.Month)
returns month starting from 0, adding 1 to the result would give the correct output. And keep in mind to subtract 1 when setting the month.Or use the constant variables provided.
最好使用
零......
It would be better to use
which is zero ...