Skip to content

DateUtil 日期工具类 发现一个错误! #584

@chorar

Description

@chorar

public static int getMonthLength(@NotNull final Date date) {
    int year = get(date, Calendar.YEAR);
    int month = get(date, Calendar.MONTH);
    return getMonthLength(year, month);
}

public static int getMonthLength(int year, int month) {
    if ((month < 1) || (month > 12)) {
        throw new IllegalArgumentException("Invalid month: " + month);
    }
    if (month == 2) {
        return isLeapYear(year) ? 29 : 28;
    }
    return MONTH_LENGTH[month];
}

因为
  int month = get(date, Calendar.MONTH); 获取的月份应该是 0-11 之间
所以
  结果是错误的

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions