判断传入日期,是否为月末最后一天

工作场景需要,根据传入日期,判断是否月末最后一天,从而确认展示的数据是否包含传入月

/**
 * 判断给定日期是否为月末的一天
 *
 * @param date
 * @return true:是|false:不是
 */
public static boolean isLastDayOfMonth(String date) {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    ParsePosition pos = new ParsePosition(0);//表示索引从第几个开始解析字符串
    Date strtodate = formatter.parse(date, pos);

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(strtodate);
    calendar.set(Calendar.DATE, (calendar.get(Calendar.DATE) + 1));
    if (calendar.get(Calendar.DAY_OF_MONTH) == 1) {
        return true;
    }
    return false;
}

例子:

isLastDayOfMonth("2018-06-30")//返回true
isLastDayOfMonth("2018-06-20")//返回false

猜你喜欢

转载自blog.csdn.net/tornado430/article/details/81873800