根据指定日期获取下个月的这一天


import cn.hutool.core.date.DateUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author:Tseng
 * @description  根据指定日期获取下个月的这一天
 * @since: JDK1.8
 * @version: 1.0
 * @date: 2022-05-09
 * @Copyright © 2022 Tseng
 */
public class NextEffectiveDate {

    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
    public static int month_static = 12;

    public static void main(String[] args) throws ParseException {
        String date = "2022-01-31 16:16:16:1";
        System.out.println("原始日期:"+date);
        System.out.println(nextEffectiveDate(sdf.parse(date)));
    }

    /**
     * 根据指定日期获取下个月的这一天
     * 特殊例子:如果是月末31号或30号,而下个月没有这一天,将获取下月月末。
     *      如:2022/1/31获取下个月这一天结果为2022/2/28
     * @param effectiveDate
     * @return
     * @throws ParseException
     */
    public static Date nextEffectiveDate(Date effectiveDate) throws ParseException {
        int year = DateUtil.year(effectiveDate);
        int nextMonth =  DateUtil.month(effectiveDate)+1+1;
        int day = DateUtil.dayOfMonth(effectiveDate);

        String time = DateUtil.formatTime(effectiveDate);
        int millisecond = DateUtil.millisecond(effectiveDate);
        if (nextMonth > month_static) {
            nextMonth = 1;
            year += 1;
        }
        String nextEffectiveDay = appendDate(year, nextMonth, day);
        if (!isValidDate(nextEffectiveDay)) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(effectiveDate);
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH, nextMonth);
            calendar.set(Calendar.DAY_OF_MONTH, 0);
            nextEffectiveDay = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
        }
        nextEffectiveDay = nextEffectiveDay + " "+ time +":"+millisecond;
        System.out.println("下月日期:"+nextEffectiveDay);
        return sdf.parse(nextEffectiveDay);
    }
    /**
     * 日期拼接
     */
    public static String appendDate(int year, int month, int day) {
        StringBuilder dateStr = new StringBuilder();
        dateStr.append(year)
                .append('-')
                .append(month > 9 ? month : "0"+month)
                .append('-')
                .append(day > 9 ? day : "0"+day);
        return dateStr.toString();
    }
    /**
     * 判断一个日期是否合法
     */
    public static boolean isValidDate(String dateStr) {
        Boolean convertStatus = true;
        //指定日期格式为四位年、两位月份、两位日期,注意 yyyy-MM-dd 区分大小写
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //设置lenient为false,否则SimpleDateFormat会比较宽松地验证日期。比如:2020-02-29会被接受,并转换成2020-03-01
        sdf.setLenient(false);
        try {
            sdf.parse(dateStr);
        } catch (ParseException e) {
            //日期格式不正确
            convertStatus = false;
        }
        return convertStatus;
    }
}

猜你喜欢

转载自blog.csdn.net/piaomiao_/article/details/124669559