Java 的 Calendar 类出现意外行为

发布于 2024-12-14 07:54:26 字数 565 浏览 0 评论 0原文

我有以下代码来获取当前系统日期的不同部分(本例为 2011 年 10 月 11 日)。

Calendar now = Calendar.getInstance();
String dt = ""+now.get(now.DATE)+"-"+now.get(now.MONTH)+"-"+now.get(now.YEAR);

在这里,DATEYEAR 字段给出了预期的值,但 MONTH 字段给出了意外的结果,首先我不知道 MONTH 字段从零开始,因此当前月份为 11 将为我提供 10。现在,如果我使用 now.get(now.MONTH+1) ,它会返回 46。简单地使用 now.MONTH 而不是使用 get 方法会得到 2

那么,我在这里做错了什么?它不应该是日历类中的错误。

请注意,我使用的是 JDK 7。

I have following code to get different parts of current system Date (10-11-2011 for this case).

Calendar now = Calendar.getInstance();
String dt = ""+now.get(now.DATE)+"-"+now.get(now.MONTH)+"-"+now.get(now.YEAR);

Here, DATE and YEAR fields are giving values as expected but MONTH field is giving unexpected results, firstly I didn't knew that MONTH field starts with zero, so having current month as 11 will give me 10. Now, if I use now.get(now.MONTH+1) than it returns 46. And using simply now.MONTH instead of using get method gives 2.

So, what am I doing wrong here? it shouldn't be a bug in Calendar class.

Note that I'm using JDK 7.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱的那么颓废 2024-12-21 07:54:26

您需要 now.get(Calendar.MONTH) + 1

now.get(Calendar.MONTH) 返回从 0 开始的月份。您需要在结果中加 1。如果执行 now.get(Calendar.MONTH + 1),您将得到除月份以外的其他内容,因为您不再将 MONTH 常量传递给 get 方法。 get 只接受一个 int 作为参数。常量 MONTH 表示“我想要获取月份”。常量 DATE 表示“我想获取日期”。它们的价值没有任何意义。 MONTH 为 2,3 为 WEEK_OF_YEAR。

另请注意,应使用类名而不是类的实例来访问静态变量(或常量)。

您是否考虑过使用SimpleDateFormat?这是使用特定模式格式化日期的类:

new SimpleDateFormat("dd/MM/yyyy").format(now.getTime());

You need now.get(Calendar.MONTH) + 1.

now.get(Calendar.MONTH) returns the month starting at 0. And you need to add 1 to the result. If you do now.get(Calendar.MONTH + 1), you're getting something other than the month, because you don't pass the MONTH constant to the get method anymore. get just takes an int as parameter. The constant MONTH means "I want to get the month". The constant DATE means "I want to get the date". Their value has no meaning. MONTH is 2, and 3 is WEEK_OF_YEAR.

Also note that static variables (or constants) should be accessed using the class name, and not an instance of the class.

Have you considered using SimpleDateFormat? That's the class to use to format a Date using a specific pattern:

new SimpleDateFormat("dd/MM/yyyy").format(now.getTime());
倾城°AllureLove 2024-12-21 07:54:26

根据 Javadoc,月份是零索引的:

月份用0到11之间的整数表示; 0 是一月,1 是二月,依此类推;因此 11 是 12 月。

日历 类似

公历和儒略历中一年的第一个月是 JANUARY,即 0;最后一个取决于一年中有多少个月。

您需要添加 1 以数字方式显示它或使用 java.text.SimpleDateFormat 它将自动为您执行此操作。

如果您对日期进行了大量工作,那么我建议使用 Joda 时间。它比 Java 核心库的日期/时间处理要好得多。

Joda-Time 提供了 Java 日期和时间类的优质替代品。该设计允许使用多个日历系统,同时仍然提供简单的 API。 “默认”日历是 XML 使用的 ISO8601 标准。格里高利体系、儒略体系、佛教体系、科普特体系、埃塞俄比亚体系和伊斯兰教体系也包括在内,我们欢迎进一步补充。支持的类包括时区、持续时间、格式和解析。

Month is zero-indexed according to the Javadoc:

A month is represented by an integer from 0 to 11; 0 is January, 1 is February, and so forth; thus 11 is December.

and similarly for Calendar

The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0; the last depends on the number of months in a year.

You need to add 1 to it to display it numerically or use java.text.SimpleDateFormat which will do this automatically for you.

If you're doing a lot of work with dates, then I suggest using Joda time instead. It is much better than the Java core library date/time handling.

Joda-Time provides a quality replacement for the Java date and time classes. The design allows for multiple calendar systems, while still providing a simple API. The 'default' calendar is the ISO8601 standard which is used by XML. The Gregorian, Julian, Buddhist, Coptic, Ethiopic and Islamic systems are also included, and we welcome further additions. Supporting classes include time zone, duration, format and parsing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文