JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-31属于第五周

本文介绍了如何在JAVA中处理月份中的日期,将其正确分配到第1至第5周,例如1-7号被归为第一周,29-31号归为第五周。通过编程解决此类日期逻辑问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public static final String DATEFORMAT_PATTERN_THREE = "yyyy-MM-dd";

/**
	 * 根据时间返回对应的周  传入时间格式 yyyy-MM-dd
	 *
	 * @param day 2019-07-01  2019-07-31
	 * @return 2019-07-w1  2019-07-w5
	 */
	public static String transDay(String day){
		String num = day.substring(8,10);
		String flag = "w1";
		switch (num){
			case "01":
			case "02":
			case "03":
			case "04":
			case "05":
			case "06":
			case "07":
				flag = "w1";
				break;
			case "08":
			case "09":
			case "10":
			case "11":
			case "12":
			case "13":
			case "14":
				flag = "w2";
				break;
			case "15":
			case "16":
			case "17":
			case "18":
			case "19":
			case "20":
			case "21":
				flag = "w3";
				break;
			case "22":
			case "23":
			case "24":
			case "25":
			case "26":
			case "27":
			case "28":
				flag = "w4";
				break;
			case "29":
			case "30":
			case "31":
				flag = "w5";
				break;

		}
		return day.substring(0,8)+flag;
	}

	public static String getLastWeekStart(Date now) {
		String startYear = "";
		String startMonth = "";
		String startDay = "";

		SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT_PATTERN_THREE);
		String date = sdf.format(now);
		Calendar cl = Calendar.getInstance();
		cl.setTime(now);
		String week = transDay(date).substring(8,10);
		switch (week){
			case "w1" :
				cl.add(Calendar.MONTH,-1);
				if((cl.get(Calendar.MONTH)+1)==2) {
					if(isLeapYear(cl.get(Calendar.YEAR))){
						startDay = "29";
					}else{
						startDay = "22";
					}
				}else {
					startDay = "29";
				}
				break;
			case "w2" :
				startDay = "01";
				break;
			case "w3" :
				startDay = "08";
				break;
			case "w4" :
				startDay = "15";
				break;
			case "w5" :
				startDay = "22";
				break;
		}
		startYear = cl.get(Calendar.YEAR)+"";    //获取年
        if((cl.get(Calendar.MONTH) + 1)<10){
            startMonth = "0"+(cl.get(Calendar.MONTH) + 1);   //获取月份,0表示1月份
        }else{
            startMonth = ""+(cl.get(Calendar.MONTH) + 1);
        }

		return startYear +"-" +startMonth + "-" + startDay + " 00:00:000" ;

	}

	public static String getLastWeekEnd(Date now) {
		String endYear = "";
		String endMonth = "";
		String endDay = "";

		SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT_PATTERN_THREE);
		String date = sdf.format(now);
		Calendar cl = Calendar.getInstance();
		cl.setTime(now);
		String week = transDay(date).substring(8,10);
		switch (week){
			case "w1" :
				cl.add(Calendar.MONTH,-1);
				endDay = cl.getActualMaximum(Calendar.DAY_OF_MONTH)+"";
				break;
			case "w2" :
				endDay = "07";
				break;
			case "w3" :
				endDay = "14";
				break;
			case "w4" :
				endDay = "21";
				break;
			case "w5" :
				endDay = "28";
				break;
		}
		endYear = cl.get(Calendar.YEAR)+"";    //获取年
		if((cl.get(Calendar.MONTH) + 1)<10){
			endMonth = "0"+(cl.get(Calendar.MONTH) + 1);   //获取月份,0表示1月份
		}else{
			endMonth = ""+(cl.get(Calendar.MONTH) + 1);
		}

		return endYear +"-" +endMonth + "-" + endDay + " 23:59:999" ;
	}

	public static boolean isLeapYear(int year ){
		if(year % 4 != 0 || year % 100 == 0 && year % 400 != 0) {
			return false;
		}else {
			return true;
		}
	}


	/**
	 * 根据开始日期 结束日期 获取相关周
	 * @param startTime
	 * @param endTime
	 * @return
	 */

	public static List<String> getListTransDay(String startTime,String endTime){
		SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT_PATTERN_THREE);

		List<String> result = new ArrayList<>();
		try {
			Date sDate = sdf.parse(startTime);
			Date eDate = sdf.parse(endTime);
			Calendar cls = Calendar.getInstance();
			cls.setTime(sDate);

			Calendar cle = Calendar.getInstance();
			cle.setTime(eDate);

			String tempTime = null;
			String trans = null;


			while(!(cls.get(Calendar.YEAR)+ ""+cls.get(Calendar.MONTH) +""+ cls.get(Calendar.DATE)+"").equals
					(cle.get(Calendar.YEAR) + ""+cle.get(Calendar.MONTH) +""+(cle.get(Calendar.DATE)+1)+"")
					)
			{
				tempTime = sdf.format(cls.getTime());
				trans = transDay(tempTime);
				result.add(trans);
				cls.add(Calendar.DATE,1);
			}

		} catch (ParseException e) {
			e.printStackTrace();
		}

		result = result.stream().distinct().collect(Collectors.toList());
		return result;
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值