获取时间范围内的每天的日期,格式“yyyyMMdd”

Test 方法测试

@Test
	public void dateList() throws IOException, ParseException {
		String beginDay = "20191101";
		String endDay = "20191209";
		List<String> dateList = new ArrayList<>();
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
		Date beginDate = sdf1.parse(beginDay);
		Date endDate = sdf1.parse(endDay);
		Calendar beginCalendar = Calendar.getInstance();
		Calendar endCalendar = Calendar.getInstance();
		beginCalendar.setTime(beginDate);
		endCalendar.setTime(endDate);
		int maxDate = 0;
			if (endCalendar.get(Calendar.YEAR) == beginCalendar.get(Calendar.YEAR)) {
				maxDate = endCalendar.get(Calendar.DAY_OF_YEAR) - beginCalendar.get(Calendar.DAY_OF_YEAR);
			} else if (endCalendar.get(Calendar.YEAR) > beginCalendar.get(Calendar.YEAR)) {
				int endNumber = endCalendar.get(Calendar.DAY_OF_YEAR);
				int beginNumber = DateUtils.getYearDays(Integer.valueOf(beginDay.substring(0, 4)))
						- beginCalendar.get(Calendar.DAY_OF_YEAR);
				maxDate = endNumber + beginNumber;
			}
		for (int i = 0; i <= maxDate; i++) {
			dateList.add(sdf1.format(beginCalendar.getTime()));
			beginCalendar.add(Calendar.DATE, 1);
		}
		for (String strDate : dateList) {
			System.out.println(strDate);
		}
	}

测试结果展示

20191101
20191102
20191103
20191104
20191105
20191106
20191107
20191108
20191109
20191110
20191111
20191112
20191113
20191114
20191115
20191116
20191117
20191118
20191119
20191120
20191121
20191122
20191123
20191124
20191125
20191126
20191127
20191128
20191129
20191130
20191201
20191202
20191203
20191204
20191205
20191206
20191207
20191208
20191209

 

封装好的方法

/**
	 * 获取日期范围内每天的日期
	 * @param beginDay 格式“yyyyMMdd”
	 * @param endDay 格式“yyyyMMdd”
	 * @return
	 */
	public List<String> getStrDateList(String beginDay,String endDay){
		List<String> dateList = new ArrayList<>();
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
		Date beginDate;
		try {
			beginDate = sdf1.parse(beginDay);
			Date endDate = sdf1.parse(endDay);
			Calendar beginCalendar = Calendar.getInstance();
			Calendar endCalendar = Calendar.getInstance();
			beginCalendar.setTime(beginDate);
			endCalendar.setTime(endDate);
            int maxDate = 0;
			if (endCalendar.get(Calendar.YEAR) == beginCalendar.get(Calendar.YEAR)) {
				maxDate = endCalendar.get(Calendar.DAY_OF_YEAR) - beginCalendar.get(Calendar.DAY_OF_YEAR);
			} else if (endCalendar.get(Calendar.YEAR) > beginCalendar.get(Calendar.YEAR)) {
				int endNumber = endCalendar.get(Calendar.DAY_OF_YEAR);
				int beginNumber = DateUtils.getYearDays(Integer.valueOf(beginDay.substring(0, 4)))
						- beginCalendar.get(Calendar.DAY_OF_YEAR);
				maxDate = endNumber + beginNumber;
			}
			for (int i = 0; i <= maxDate; i++) {
				dateList.add(sdf1.format(beginCalendar.getTime()));
				beginCalendar.add(Calendar.DATE, 1);
			}
		} catch (ParseException e) {
			
			e.printStackTrace();
		}	
		return dateList;		
	}

 

获取时间范围之间的天数

/**
	 * @param date1
	 * @param date2
	 * @return
	 * @throws ParseException
	 */
	public static int betweenDays(String beginDay, String endDay) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date date1 = sdf.parse(beginDay);
		Date date2 = sdf.parse(endDay);
		System.out.println(date1);
		Calendar begin = DateUtil.toCalendar(date1);
		Calendar end =  DateUtil.toCalendar(date2);		
		long msBegin = begin.getTimeInMillis();
		long msEnd = end.getTimeInMillis();
		long between_days = (msEnd - msBegin) / (1000 * 3600 * 24);
		between_days = Math.abs(between_days);
		return Integer.parseInt(String.valueOf(between_days));
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值