工具类---日期工具类内找对应星期的日期、每隔几天的日期

一、输入一段日期,和星期,得到一段日期内指定星期几的那些天的日期数

import org.springframework.stereotype.Component;

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

/**
* @ClassName: WeekDayUtil 
* @Description: 得到一段日期内指定星期几的那些天的日期数
* @Author: zhouhong
* @Create: 2021-06-01 15:44
**/
@Component
public class WeekDayUtil {
/**
* 输入一个日期的时间段,以及相应的星期数,获得这些星期的日期
*/
private static Map<Integer, Integer> weekNumberMap = new HashMap<Integer, Integer>();
static {
weekNumberMap.put(0,1);
weekNumberMap.put(1,2);
weekNumberMap.put(2,3);
weekNumberMap.put(3,4);
weekNumberMap.put(4,5);
weekNumberMap.put(5,6);
weekNumberMap.put(6,7);
}
public List<String> getDates(String dateFrom, String dateEnd, List<Integer> weekDays) {
long time;
long perDayMilSec = 24L * 60 * 60 * 1000;
List<String> dateList = new ArrayList<String>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// 需要查询的星期系数
String strWeekNumber = weekForNum(weekDays);
try {
dateFrom = sdf.format(sdf.parse(dateFrom).getTime() - perDayMilSec);
while (true) {
time = sdf.parse(dateFrom).getTime();
time = time + perDayMilSec;
Date date = new Date(time);
dateFrom = sdf.format(date);
if (dateFrom.compareTo(dateEnd) <= 0) {
// 查询的某一时间的星期系数
Integer weekDay = dayForWeek(date);
// 判断当期日期的星期系数是否是需要查询的
if (strWeekNumber.contains(weekDay.toString())) {
dateList.add(dateFrom);
}
} else {
break;
}
}
} catch (ParseException e) {
e.printStackTrace();
}
return dateList;
}
// 等到当期时间的周系数。星期日:1,星期一:2,星期二:3,星期三:4,星期四:5,星期五:6,星期六:7
public Integer dayForWeek(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_WEEK);
}
/**
* 得到对应星期的系数 0:1,星期
*/
public String weekForNum(List<Integer> weekDays) {
// 返回结果为组合的星期系数
String weekNumber = "";
for (Integer weekDay : weekDays) {
weekNumber = weekNumber + "" + getWeekNum(weekDay).toString();
}
return weekNumber;
}
// 将星期转换为对应的系数 0,星期日; 1,星期一; 2....
public Integer getWeekNum(int strWeek) {
return weekNumberMap.get(strWeek);
}
/*public static void main(String[] args) {
//输出从2021-06-01到2021-07-22之间的所有星期一和星期二的日期。
List<Integer> daysOfOneWeek = new ArrayList<Integer>();
daysOfOneWeek.add(1);
daysOfOneWeek.add(2);
List<String> daysNeedBookList = getDates("2021-06-01", "2021-07-22", daysOfOneWeek);
for (String s : daysNeedBookList) {
System.out.println(s);
}
}*/
}

二、找到一段日期内每隔几天的日期

package com.honyar.iot.ibs.smartpatrol.modular.common.util;

import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: ibs
 * @Description: 获取一个时间段内每隔几天的所有天数的日期
 * @Author: zhouhong
 * @Create: 2021-06-01 22:46
 **/
@Component
public class DaysOfDateUtil {
    public static ArrayList<String> getIntervals(String startTime, String endTime, int calUnit, int interval) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        ArrayList<String> times = new ArrayList<>();
        Date startDate = sdf.parse(startTime);
        Date endDate = sdf.parse(endTime);
        Calendar startCal = Calendar.getInstance();
        startCal.setTime(startDate);
        times.add(startTime);
        while (startCal.getTime().before(endDate))  {
            startCal.add(calUnit, interval);
            times.add(sdf.format(startCal.getTime()));
        }
        return times;
    }
}

备注:当最后一天包含在日期内,结果正确,反之:需要减去最后一个日期

 


已有 0 条评论

    欢迎您,新朋友,感谢参与互动!