传入日期时间,获取下一个月的对应时间

例如用于获取下个月的当前时间,则传入new Date()当前时间即可。

 /**
     * 传入日期时间,获取下一个月的日期时间
     * 描述:<描述函数实现的功能>.
     * @param repeatDate 当前时间
     * @return
     */
    public static String getPreMonth(String repeatDate) {
        String lastMonth = "";
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int year = Integer.parseInt(repeatDate.substring(0, 4));
        String monthsString = repeatDate.substring(4, 6);
        int month;
        if ("0".equals(monthsString.substring(0, 1))) {
            month = Integer.parseInt(monthsString.substring(1, 2));
        } else {
            month = Integer.parseInt(monthsString.substring(0, 2));
        }
        cal.set(year,month,Calendar.DATE);
        lastMonth = dft.format(cal.getTime());
        return lastMonth;
    }

猜你喜欢

转载自www.cnblogs.com/laifw/p/10388529.html