使用JAVA获取每一周中的七个日期(一周中的每一天)

1.GetMW类

public class GetMW {

    public static void main(String[] args){
        System.out.println(getDates("2023;15"));
    }
    public static List getDates(String yearWeek){
        String year=yearWeek.split(";")[0];
        String week=yearWeek.split(";")[1];
        SimpleDateFormat sdf = new SimpleDateFormat("E");//获取日期在星期几
        Date date = null;
        try {
            date = new SimpleDateFormat("yyyy-MM-dd").parse(year+"-01-01");//获取当年第一天
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String fristWeek1=sdf.format(date);//获取日期在星期几
        int firstWeek=1;
        switch (fristWeek1){//转换成int
            case "星期一":
                firstWeek=2;
                break;
            case "星期二":
                firstWeek=3;
                break;
            case "星期三":
                firstWeek=4;
                break;
            case "星期四":
                firstWeek=5;
                break;
            case "星期五":
                firstWeek=6;
                break;
            case "星期六":
                firstWeek=7;
                break;
            case "星期日":
                firstWeek=1;
                break;
            default:
                break;
        }
        WeekDate weekD=new WeekDate();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        if(firstWeek==1){
        //如果当年第一天是星期天
            int day1=Integer.parseInt(week)*7-7;
            //获取第n周星期天为day1
            int day7=Integer.parseInt(week)*7-1;
            //获取第n周星期六为day7
            List getDate = getList(year, weekD, day1, day7);
            if (getDate != null) return getDate;

        }else{
            if(Integer.parseInt(week)==1){
            //如果01-01不是星期天求当年第一周
                try {
                    Date date1=new SimpleDateFormat("yyyy-MM-dd").parse(year+"-01-01");
                    Date startDate=DateUtils.addDays(date1, -(firstWeek-1));
                    //该周第一天在上一年
                    Date endDate=DateUtils.addDays(date1, (7-firstWeek));
                    //该周最后一天
                    weekD.setStartDate(simpleDateFormat.format(startDate));
                    weekD.setEndDate(simpleDateFormat.format(endDate));
                    GetDate getDate = null;
                    return getDate.getBetweenDate(client);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }else{
            //如果01-01不是星期天求当年第n周
                int day1=(Integer.parseInt(week)-1)*7-firstWeek+1;
                int day7=Integer.parseInt(week)*7-firstWeek;
                List getDate = getList(year, weekD, day1, day7);
                if (getDate != null) return getDate;
            }
        }
        return null;
    }

    private static List getList(String year, WeekDate weekD, int day1, int day7) {
        try {
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
            Date date1=new SimpleDateFormat("yyyy-MM-dd").parse(year+"-01-01");
            Date startDate= DateUtils.addDays(date1, day1);
            Date endDate=DateUtils.addDays(date1, day7);
            weekD.setStartDate(simpleDateFormat.format(startDate));
            weekD.setEndDate(simpleDateFormat.format(endDate));
            GetDate getDate = null;
            return getDate.getBetweenDate(client);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

}

2.GetDate类

public class GetDate {
    /**
     *  获取两个日期之间的所有日期 (年月日)
     *
     * @param
     * @param
     * @return
     */
    public static List<String> getBetweenDate(WeekDate weekD){
        String start=weekD.getStartDate();
        String end=weekD.getEndDate();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 声明保存日期集合
        List<String> list = new ArrayList<String>();
        try {
            // 转化成日期类型
            Date startDate = sdf.parse(start);
            Date endDate = sdf.parse(end);

            //用Calendar 进行日期比较判断
            Calendar calendar = Calendar.getInstance();
            while (startDate.getTime()<=endDate.getTime()){
                // 把日期添加到集合
                list.add(sdf.format(startDate));
                // 设置日期
                calendar.setTime(startDate);
                //把日期增加一天
                calendar.add(Calendar.DATE, 1);
                // 获取增加后的日期
                startDate=calendar.getTime();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return list;
    }

}

3.WeekDate 类

public class WeekDate {
    private String startDate;
    private String endDate;

    public WeekDate() {
    }

    public String getStartDate() {
        return startDate;
    }

    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }

    public String getEndDate() {
        return endDate;
    }

    public void setEndDate(String endDate) {
        this.endDate = endDate;
    }

    @Override
    public String toString() {
        return "WeekDate{" +
                "startDate='" + startDate + '\'' +
                ", endDate='" + endDate + '\'' +
                '}';
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Java 的 Calendar 类来获取两个时间点之间的所有日期,具体步骤如下: 1. 首先,需要将两个时间点转换为 Calendar 对象,并获取它们的年、月、日等信息。 2. 然后,通过循环遍历两个时间点之间的所有日期,判断每个日期是否为一,如果是一则输出该日期的年、月、日信息。 下面是一个示例代码: ```java import java.util.Calendar; public class Main { public static void main(String[] args) { Calendar start = Calendar.getInstance(); start.set(2022, Calendar.JANUARY, 1); // 起始日期 Calendar end = Calendar.getInstance(); end.set(2022, Calendar.DECEMBER, 31); // 结束日期 Calendar current = start; while (current.compareTo(end) <= 0) { if (current.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) { int year = current.get(Calendar.YEAR); int month = current.get(Calendar.MONTH) + 1; int day = current.get(Calendar.DAY_OF_MONTH); System.out.println(year + "-" + month + "-" + day); } current.add(Calendar.DATE, 1); } } } ``` 在这个示例,我们首先定义了起始日期和结束日期,然后使用 Calendar.getInstance() 方法获取 Calendar 实例,并将起始日期和结束日期设置到 Calendar 实例。接着,我们定义一个 current 变量,用于循环遍历起始日期和结束日期之间的所有日期,判断每个日期是否为一,如果是一则输出该日期的年、月、日信息。需要注意的是,在循环,我们每次将 current 变量加一天,直到到达结束日期为止。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值