获取指定年份到当前年的所有月初及月末日期(年月日)
后端
162 ·
0 ·
2022-06-24 10:33:48
最新编辑原因:

包:

import org.apache.commons.lang.time.DateFormatUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

函数:

public static Map<String, String> getYearMonthStart(Integer year) {
    Map<String, String> map = new HashMap<>();

    String[] split = DateFormatUtils.format(new Date(), "yyyy-MM").split("-");
    Integer currentYear = Integer.valueOf(split[0]);
    Integer currentMonth = Integer.parseInt(split[1]);
    if (year > currentYear) {
        throw new RuntimeException("不支持未来年份");
    }

    // 当前年
    if (currentYear.equals(year)) {
        putCurrentYear(map, currentYear, currentMonth);
    } else {
        // 处理历史年份
        int difference = currentYear - year;
        Integer startYear = currentYear - difference;
        for (Integer y = startYear; y < currentYear; y++) {
            for (int i = 1; i <= 12; i++) {
                // 获取指定年的12个月份
                Integer month = i;
                Date startDate = getFirstDayOfMonth(y, month);
                Date endDate = getLastDayOfMonth(y, month);
                map.put(DateFormatUtils.format(startDate, "yyyy-MM-dd"), DateFormatUtils.format(endDate, "yyyy-MM-dd"));
            }
        }
        // 处理当前年份
        putCurrentYear(map, currentYear, currentMonth);
    }
    return map;
}

private static void putCurrentYear(Map<String, String> map, Integer currentYear, Integer currentMonth) {
    for (int i = 1; i <= currentMonth; i++) {
        Integer month = i;
        Date startDate = getFirstDayOfMonth(currentYear, month);
        Date endDate = getLastDayOfMonth(currentYear, month);
        map.put(DateFormatUtils.format(startDate, "yyyy-MM-dd"), DateFormatUtils.format(endDate, "yyyy-MM-dd"));
    }
}

//获取目标年份中目标月的第一天的开始时间
public static Date getFirstDayOfMonth(Integer year, Integer month) {
    final Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, year);
    cal.set(Calendar.MONTH, month - 1);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    final int start = Calendar.YEAR;
    cal.set(Calendar.DAY_OF_MONTH, start);
    return cal.getTime();
}

//获取目标年份中目标月的最后一天
public static Date getLastDayOfMonth(Integer year, Integer month) {
    final Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, year);
    cal.set(Calendar.MONTH, month - 1);
    cal.set(Calendar.HOUR_OF_DAY, 23);
    cal.set(Calendar.MINUTE, 59);
    cal.set(Calendar.SECOND, 59);
    final int last = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    cal.set(Calendar.DAY_OF_MONTH, last);
    return cal.getTime();
}

测试方法:

    public static void main(String[] args) {
        Map<String, String> stringStringMap = getYearMonthStart(2019);
        stringStringMap.keySet().stream().sorted().
                forEach(k -> {
                    System.out.println("startDate:" + k + " , endDate:" + stringStringMap.get(k));
                });
    }

输出:

startDate:2019-01-01 , endDate:2019-01-31
startDate:2019-02-01 , endDate:2019-02-28
startDate:2019-03-01 , endDate:2019-03-31
startDate:2019-04-01 , endDate:2019-04-30
startDate:2019-05-01 , endDate:2019-05-31
startDate:2019-06-01 , endDate:2019-06-30
startDate:2019-07-01 , endDate:2019-07-31
startDate:2019-08-01 , endDate:2019-08-31
startDate:2019-09-01 , endDate:2019-09-30
startDate:2019-10-01 , endDate:2019-10-31
startDate:2019-11-01 , endDate:2019-11-30
startDate:2019-12-01 , endDate:2019-12-31
startDate:2020-01-01 , endDate:2020-01-31
startDate:2020-02-01 , endDate:2020-02-29
startDate:2020-03-01 , endDate:2020-03-31
startDate:2020-04-01 , endDate:2020-04-30
startDate:2020-05-01 , endDate:2020-05-31
startDate:2020-06-01 , endDate:2020-06-30
startDate:2020-07-01 , endDate:2020-07-31
startDate:2020-08-01 , endDate:2020-08-31
startDate:2020-09-01 , endDate:2020-09-30
startDate:2020-10-01 , endDate:2020-10-31
startDate:2020-11-01 , endDate:2020-11-30
startDate:2020-12-01 , endDate:2020-12-31
startDate:2021-01-01 , endDate:2021-01-31
startDate:2021-02-01 , endDate:2021-02-28
startDate:2021-03-01 , endDate:2021-03-31
startDate:2021-04-01 , endDate:2021-04-30
startDate:2021-05-01 , endDate:2021-05-31
startDate:2021-06-01 , endDate:2021-06-30
startDate:2021-07-01 , endDate:2021-07-31
startDate:2021-08-01 , endDate:2021-08-31
startDate:2021-09-01 , endDate:2021-09-30
startDate:2021-10-01 , endDate:2021-10-31
startDate:2021-11-01 , endDate:2021-11-30
startDate:2021-12-01 , endDate:2021-12-31
startDate:2022-01-01 , endDate:2022-01-31
startDate:2022-02-01 , endDate:2022-02-28
startDate:2022-03-01 , endDate:2022-03-31
startDate:2022-04-01 , endDate:2022-04-30
startDate:2022-05-01 , endDate:2022-05-31
startDate:2022-06-01 , endDate:2022-06-30

本作品系原创,采用《署名-非商业性使用-禁止演绎4.0 国际》许可协议.转载请说明出处
本文链接:https://www.upupor.com/u/22062402307274563584 复制

无内容

推荐阅读
暗淡蓝点 · 响应时间: 57ms · 版本:2022-08-07T16:03:28.257 · 备案号: 皖ICP备17015935号-2 · upupor.com©2022 · 项目始于2019