欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java--获取近几个月的月初和月末

程序员文章站 2022-03-03 09:06:17
...
package com.xxx.xxx.util;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author BuShuangLi
 * @date 2019/5/22
 */
public class MissingDateUtil {
	/**
	 * 查询近几个月的月初和月末
	 * @param num 近五个月 num=5
	 * @return
	 */
	public static synchronized List<Map<String, Object>> getRecentMonths(int num){
		List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();
		//当前时所在月份的第一天和最后一天
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM");
		//设置startDate
		Calendar c = Calendar.getInstance();
		//设置endDate
		Calendar c2 = Calendar.getInstance();
		for (int i = 0; i < num; i++) {
			HashMap<String, Object> dateMap = new HashMap<String, Object>();
			//设置start
			//设置为1号,当前日期既为本月第一天
			c.set(Calendar.DAY_OF_MONTH,1);
			//设置为0点0分0秒
			c.set(c.get(c.YEAR), c.get(c.MONTH), c.get(c.DAY_OF_MONTH), 0, 0, 0);
			//存入年月显示
			dateMap.put("yearMonthStr",format.format(c.getTime()));
			dateMap.put("startDate",c.getTime());
			//将c 设置为下个月
			c.add(c.DATE, -1);



			//设置end 为当前月的月底 23时59分59秒
			c2.set(Calendar.DAY_OF_MONTH,1);
			//设置时间 23时59分59秒
			c2.set(c2.get(c.YEAR), c2.get(c.MONTH), c2.get(c.DAY_OF_MONTH), 23, 59, 59);
			//获得当前月最后一天
			c2.add(Calendar.MONTH, 1);
			c2.set(Calendar.DAY_OF_MONTH, 0);
			//存入map
			dateMap.put("endDate",c2.getTime());
			//获取当前时间的下一个月
			c2.add(Calendar.MONTH, -1);
			//将c2设置为当前时间上一个月
			c2.set(c.get(c.YEAR), c.get(c.MONTH), c.get(c.DAY_OF_MONTH));
			//放入集合
			maps.add(dateMap);

		}
	}
}

数据格式

[{
	endDate = Fri May 31 23: 59: 59 CST 2019,
	yearMonthStr = 2019 - 05,
	startDate = Wed May 01 00: 00: 00 CST 2019
}, {
	endDate = Tue Apr 30 23: 59: 59 CST 2019,
	yearMonthStr = 2019 - 04,
	startDate = Mon Apr 01 00: 00: 00 CST 2019
}, {
	endDate = Sun Mar 31 23: 59: 59 CST 2019,
	yearMonthStr = 2019 - 03,
	startDate = Fri Mar 01 00: 00: 00 CST 2019
}, {
	endDate = Thu Feb 28 23: 59: 59 CST 2019,
	yearMonthStr = 2019 - 02,
	startDate = Fri Feb 01 00: 00: 00 CST 2019
}, {
	endDate = Thu Jan 31 23: 59: 59 CST 2019,
	yearMonthStr = 2019 - 01,
	startDate = Tue Jan 01 00: 00: 00 CST 2019
}]