diff --git a/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/utils/YearMonthUtils.java b/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/utils/YearMonthUtils.java new file mode 100644 index 0000000..905dae2 --- /dev/null +++ b/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/utils/YearMonthUtils.java @@ -0,0 +1,80 @@ +/** + * Copyright (c) 2018 人人开源 All rights reserved. + * + * https://www.renren.io + * + * 版权所有,侵权必究! + */ + +package com.elink.esua.epdc.commons.tools.utils; + +import com.elink.esua.epdc.commons.tools.validator.AssertUtils; + +import java.text.SimpleDateFormat; +import java.util.*; + +/** + * 获取月份,日期数组 + * + * @author Mark sunlightcs@gmail.com + * @since 1.0.0 + */ +public class YearMonthUtils { + + /** + * 获取两个时间节点之间的月份列表 + * @param minDate + * @param maxDate + * @return + */ + public static List getMonthBetween(String minDate, String maxDate){ + ArrayList result = new ArrayList(); + try { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月 + + Calendar min = Calendar.getInstance(); + Calendar max = Calendar.getInstance(); + min.setTime(sdf.parse(minDate)); + min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1); + + max.setTime(sdf.parse(maxDate)); + max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2); + + Calendar curr = min; + while (curr.before(max)) { + result.add(sdf.format(curr.getTime())); + curr.add(Calendar.MONTH, 1); + } + } catch (Exception e) { + e.printStackTrace(); + } + + return result; + } + + /** + * java 获取 获取某年某月 所有日期(yyyy-mm-dd格式字符串) + * @param year + * @param month + * @return + */ + public static List getMonthFullDay(int year , int month){ + SimpleDateFormat dateFormatYYYYMMDD = new SimpleDateFormat("yyyy-MM-dd"); + List fullDayList = new ArrayList<>(32); + // 获得当前日期对象 + Calendar cal = Calendar.getInstance(); + cal.clear();// 清除信息 + cal.set(Calendar.YEAR, year); + // 1月从0开始 + cal.set(Calendar.MONTH, month-1 ); + // 当月1号 + cal.set(Calendar.DAY_OF_MONTH,1); + int count = cal.getActualMaximum(Calendar.DAY_OF_MONTH); + for (int j = 1; j <= count ; j++) { + fullDayList.add(dateFormatYYYYMMDD.format(cal.getTime())); + cal.add(Calendar.DAY_OF_MONTH,1); + } + return fullDayList; + } + +}