java获取月的第一天和最后一天

在Java中获取月的第一天和最后一天主要是通过Calendar对象来实现。

/**
 * 获取月的第一天
 *
 * @param month 月
 */
private String getMonthBeginDate(String month) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar c = Calendar.getInstance();
    c.set(Calendar.MONTH, Integer.valueOf(month) - 1);
    c.set(Calendar.DAY_OF_MONTH, 1);
    return sdf.format(c.getTime());
}

/**
 * 获取月的最后一天
 *
 * @param month 月
 */
private String getMonthEndDate(String month) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar c = Calendar.getInstance();
    c.set(Calendar.MONTH, Integer.valueOf(month) - 1);
    c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
    return sdf.format(c.getTime());
}

也可以把入参换成整数类型,就不用在方法中转换类型了。

"守好你的心事,不要告诉任何人。"

猜你喜欢

转载自www.cnblogs.com/yanggb/p/11834455.html