Java:获取两个区间内 为周几或星期符合 的所有日期,指定日期 使用周数计算出相对应的工作日
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class TestPan11 {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
List<String> dayOfWeekWithinDateInterval = getDayOfWeekWithinDateInterval("2020-12-14", "2021-01-14", 2);
System.out.println("获取两个区间内 为 周二的所有日期 : "+dayOfWeekWithinDateInterval);
String endDate= selectBeginAndEndDate("2020-12-14", 4);
System.out.println("指定日期加上 * 天 算出结束日期 : "+endDate);
List<String> dates = getBeginAndEndDateBetween("2020-12-14", "2021-01-11");
System.out.println(dates);
String week = getWeek(sdf.parse("2020-12-14"));
System.out.println("指定日期,推算计算出这个日期为 : "+week);
String scheduleWeek = "2,4";
List<String> strings = Arrays.asList(scheduleWeek.split(","));
Collection<String> scheduleRuleDateList = new ArrayList<>();
for (String dayWeek : strings) {
List<String> dayOfWeekWithinDateInterva12 = getDayOfWeekWithinDateInterval("2020-12-14", "2021-01-11", Integer.parseInt(dayWeek));
scheduleRuleDateList.addAll(dayOfWeekWithinDateInterva12);
}
System.out.println("指定日期 + 周数 算出区间内所有日期 得出集合日期 : "+scheduleRuleDateList);
}
public static String selectBeginAndEndDate(String startDate,long weekNum) {
try {
weekNum = weekNum * 7-1;
Date date = sdf.parse(startDate);
long time = date.getTime();
weekNum = weekNum*24*60*60*1000;
time+=weekNum;
return sdf.format(new Date(time));
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static List<String> getDayOfWeekWithinDateInterval(String dateBegin, String dateEnd, int weekDays) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
List<String> dateResult = new ArrayList<>();
Calendar cal = Calendar.getInstance();
String[] dateInterval = {dateBegin, dateEnd};
Date[] dates = new Date[dateInterval.length];
for (int i = 0; i < dateInterval.length; i++) {
String[] ymd = dateInterval[i].split("[^\\d]+");
cal.set(Integer.parseInt(ymd[0]), Integer.parseInt(ymd[1]) - 1, Integer.parseInt(ymd[2]));
dates[i] = cal.getTime();
}
for (Date date = dates[0]; date.compareTo(dates[1]) <= 0; ) {
cal.setTime(date);
if (cal.get(Calendar.DAY_OF_WEEK) - 1 == weekDays) {
String format = sdf.format(date);
dateResult.add(format);
}
cal.add(Calendar.DATE, 1);
date = cal.getTime();
}
return dateResult;
}
public static String getWeek(Date date){
String[] weeks = {"周日","周一","周二","周三","周四","周五","周六"};
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
if(week_index<0){
week_index = 0;
}
return weeks[week_index];
}
public static List<String> getBeginAndEndDateBetween(String beginDate, String endDate) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
List<String> list = new ArrayList<String>();
Date date_start = sdf.parse(beginDate);
Date date_end = sdf.parse(endDate);
Date date =date_start;
Calendar cd = Calendar.getInstance();
while (date.getTime() <= date_end.getTime()){
list.add(sdf.format(date));
cd.setTime(date);
cd.add(Calendar.DATE, 1);
date=cd.getTime();
}
return list;
}
- 指定起始日期 : 指定结束日期 :指定周几 ,算出区间内指定周几的日期
public static List<String> getDayOfWeekWithinDateInterval(String dateBegin, String dateEnd, int weekDays) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
List<String> dateResult = new ArrayList<>();
Calendar cal = Calendar.getInstance();
String[] dateInterval = {dateBegin, dateEnd};
Date[] dates = new Date[dateInterval.length];
for (int i = 0; i < dateInterval.length; i++) {
String[] ymd = dateInterval[i].split("[^\\d]+");
cal.set(Integer.parseInt(ymd[0]), Integer.parseInt(ymd[1]) - 1, Integer.parseInt(ymd[2]));
dates[i] = cal.getTime();
}
for (Date date = dates[0]; date.compareTo(dates[1]) <= 0; ) {
cal.setTime(date);
if (cal.get(Calendar.DAY_OF_WEEK) - 1 == weekDays) {
String format = sdf.format(date);
dateResult.add(format);
}
cal.add(Calendar.DATE, 1);
date = cal.getTime();
}
return dateResult;
}
public static String selectBeginAndEndDate(String startDate,long day) throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = dateFormat.parse(startDate);
long time = date.getTime();
day = day*7*24*60*60*1000;
time+=day;
return dateFormat.format(new Date(time));
}
public static String getWeek(Date date){
String[] weeks = {"周日","周一","周二","周三","周四","周五","周六"};
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
if(week_index<0){
week_index = 0;
}
return weeks[week_index];
}
public static List<String> getBeginAndEndDateBetween(String beginDate, String endDate) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
List<String> list = new ArrayList<String>();
Date date_start = sdf.parse(beginDate);
Date date_end = sdf.parse(endDate);
Date date =date_start;
Calendar cd = Calendar.getInstance();
while (date.getTime() <= date_end.getTime()){
list.add(sdf.format(date));
cd.setTime(date);
cd.add(Calendar.DATE, 1);
date=cd.getTime();
}
return list;
}