从java日历中获取错误的月份数

时间:2017-06-13 10:08:07

标签: java

我正在尝试将当前月份作为日历从日历中获取,但是错误的月份编号。

我使用了以下代码:

public static Date getTodayDate() {
    Calendar calendar = Calendar.getInstance();
    return calendar.getTime();
}

private static int getMonthFromDate() {
    Calendar cal = Calendar.getInstance();
    cal.setTime(getTodayDate());
    return cal.get(Calendar.MONTH);
}

public static void main(String[] arps) {
    System.out.println("Current Month in Integer :: " + getMonthFromDate());
}
  

输出: - 整数当前月份:: 5

     

预期输出: - 整数当前月份:6

2 个答案:

答案 0 :(得分:2)

Calendar中的月份从零开始,即Jan = 0. documentation提供完整的详细信息。

答案 1 :(得分:0)

月份编号从0(1月)到11(12月)。

使用System.out.println("Current Month in Integer :: " + getMonthFromDate()+1);

相关问题