java 获取两个日期之间的所有周四,并计算每个周四在当月属于第几周

最近的任务很多都要计算时间,写了很多关于计算日期的,希望可以帮助有需要的人。 

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

/**
 * 
 * 获取两个日期之间的所有周四 并计算每个周四在当月是第几周
 * @author lzw
 * @Date 2019年3月6日
 */
public class demo3 {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		List<String> test = getWeekly();
		
		System.err.println(test);

	}


	public static boolean thursdayOrNot(String str) throws ParseException{
		DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sdf.parse(str);
		SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
		String currSun = dateFm.format(date);
		if (currSun.equals("星期四")) {
			return true;
		}
		return false;
	}
	

	public static String getWeek(Date date) throws Exception {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String format = sdf.format(date);
		String substring = format.substring(0, 7);
		int number = calendar.get(Calendar.WEEK_OF_MONTH);
		String week = substring + "-0" + number;
		return week;
	}
	public static List<String> getWeekly() throws Exception{
		List<String> list = findDates("2019-02-01", "2019-03-31");
		List<String> thursdayList = new ArrayList<>();
		for (String string : list) {
			boolean thursday = thursdayOrNot(string);
			if (thursday==true) {
				thursdayList.add(string);
			}
			
		}
		DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		List<String> weekList = new ArrayList<>();
		for (String str : thursdayList) {
			Date date = sdf.parse(str);
			String week = getWeek(date);
			weekList.add(week);
		}
		return weekList;
	}
	
	public static List<String> findDates(String dBegin, String dEnd) throws ParseException {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calBegin = Calendar.getInstance();
		calBegin.setTime(format.parse(dBegin));
		Calendar calEnd = Calendar.getInstance();
		calEnd.setTime(format.parse(dEnd));
		List<String> Datelist = new ArrayList<String>();
		while (format.parse(dEnd).after(calBegin.getTime())) {
			calBegin.add(Calendar.DAY_OF_MONTH, 1);
			Datelist.add(format.format(calBegin.getTime()));
		}
		Datelist.add(dBegin);
		return Datelist;
	}

}

 获取两个时间段之间的周五,并判断他的周次

public static List<String> getAllWeekly2(String startDate, String endDate) throws Exception {

		// 获取两个日期之间的所有日期
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calBegin = Calendar.getInstance();
		calBegin.setTime(format.parse(startDate));
		Calendar calEnd = Calendar.getInstance();
		calEnd.setTime(format.parse(endDate));
		List<String> Datelist = new ArrayList<String>();
		Datelist.add(startDate);
		while (format.parse(endDate).after(calBegin.getTime())) {
			calBegin.add(Calendar.DAY_OF_MONTH, 1);
			Date date = calBegin.getTime();
			SimpleDateFormat dateFm = new SimpleDateFormat("EEEE");
			String currSun = dateFm.format(date);
			if (currSun.equals("星期五")) {
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(date);
				DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				String format1 = sdf.format(date);
				String substring = format1.substring(0, 7);
				int number = calendar.get(Calendar.WEEK_OF_MONTH);
				String week = substring + "-0" + number;
				Datelist.add(week);
			}
		}

		return Datelist;
	}

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值