java - 为什么用java日历解析时会得到错误的月份

标签 java timer calendar

Date fakeDate = sdf.parse("15/07/2013 11:00 AM");
Calendar calendar = Calendar.getInstance()
calendar.setTime(fakeDate);
int currentMonth = calendar.get(Calendar.MONTH);

我得到 currentMonth == 6 而不是 7。

这是为什么?

最佳答案

因为Calendar.MONTH是从零开始的。 Why

检查docs :(总是)

Field number for get and set indicating the month. This is a calendar-specific value. 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.

关于java - 为什么用java日历解析时会得到错误的月份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25350178/

相关文章:

java - 如何在新的STS 3.7.2中运行旧的STS 2.8.0 GWT SDK项目?

java - DBCP - 不同数据库的验证查询

ios - Apple 使用什么公式来计算日历中事件的文本和背景颜色?

asp.net - ASP :Calendar DateClicked event (not just SelectionChanged)

java - 从 fragment 中检索上下文

java - 为什么客户端只有在关闭此 TLS 连接中的流后才会收到消息?

windows-phone-7 - 如何在 wp7 应用程序中设置计时器?

javascript - setTimeout 与 setInterval - 使用的最佳实践

R语言——定时器

javascript - jQuery 日期选择器 : preselect month of second datepicker