编辑代码

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

class Main {

    	public final static SimpleDateFormat DATEFORMAT_DAY = new SimpleDateFormat("yyyy-MM-dd");

	private static final SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public static void main(String[] args) throws ParseException {
        //JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。
        Date temp = timeFormat.parse("2021-04-12 23:00:00");
        System.out.println(getWeekDesc(temp));
	}
    	public static String getWeekDesc(Date currDate){
		Date lastWeekEndDate=getWeekEndDate(-1);
        System.out.println("lastWeekEndDate =" + lastWeekEndDate);
		Date currWeekEndDate=getWeekEndDate(0);
         System.out.println("currWeekEndDate =" + currWeekEndDate);
		Date nextWeekEndDate=getWeekEndDate(1);
         System.out.println("nextWeekEndDate =" + nextWeekEndDate);
		Date nextNextWeekEndDate=getWeekEndDate(2);
         System.out.println("nextNextWeekEndDate =" + nextNextWeekEndDate);
		String weekDesc="-";
		if(currDate.after(lastWeekEndDate) && currDate.before(currWeekEndDate)){
			weekDesc="本周";
		}else if(currDate.after(currWeekEndDate) && currDate.before(nextWeekEndDate)){
			weekDesc="下周";
		}else if(currDate.after(nextWeekEndDate) && currDate.before(nextNextWeekEndDate)){
			weekDesc="下下周";
		}
		return weekDesc;
	}

	/**
	 * 以当前时间获取上周、本周、下周、下下周的结束时间
	 * @param flag:-1为上周、0为本周、1为下周、2为下下周
	 * @return
	 */
	public static Date getWeekEndDate(int flag){
		SimpleDateFormat format = DATEFORMAT_DAY;
		SimpleDateFormat format1 = timeFormat;
		Calendar gc = new GregorianCalendar();
		gc.add(3, flag);
		Date week = null;
		if (gc.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
			gc.add(5, 0);
			week = gc.getTime();
		} else if (gc.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY) {
			gc.add(5, 1);
			week = gc.getTime();
		} else if (gc.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY) {
			gc.add(5, 2);
			week = gc.getTime();
		} else if (gc.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY) {
			gc.add(5, 3);
			week = gc.getTime();
		} else if (gc.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY) {
			gc.add(5, 4);
			week = gc.getTime();
		} else if (gc.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
			gc.add(5,5);
			week = gc.getTime();
		} else {
			week = gc.getTime();
		}
		String temp = format.format(week);
		temp += " 23:59:59";
		try {
			week= format1.parse(temp);
		} catch (Exception e) {
			week = null;
		}
		return week;
	}
}