From 4f4ae84330bebd465257e013633ff3976bff58d5 Mon Sep 17 00:00:00 2001 From: wanggongfeng <1305282856@qq.com> Date: Wed, 12 Oct 2022 11:18:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=A7=E5=B1=8F=E6=8E=A5=E5=8F=A3=E6=BB=A1?= =?UTF-8?q?=E6=84=8F=E5=BA=A6=E7=AD=89=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/tools/utils/YearMonthUtils.java | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/utils/YearMonthUtils.java 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; + } + +}