java获取时间段内的所有星期一

原文:https://blog.csdn.net/weixin_42228950/article/details/88308779

 

前言:

最近有个功能需要用到对于时间的操作,网上有个不错的文章,如上贴出,小编稍微改了点代码,在此对原作者表示感谢,希望能够帮助有需要的人

功能: 

传入开始时间 结束时间 星期一,即可得出这段时间有几个星期一,将时间列出。

 

代码:

package com.bos.common;

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 DateUtil {

    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        String week = "星期二";
        List<String> test = getWeekly("2020-01-07 14:19:29","2020-01-14 00:00:00",week);
        System.err.println(test);

    }


    public static boolean weekdayOrNot(String date,String week) throws ParseException{
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date currentDate = sdf.parse(date);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE");
        String currSun = simpleDateFormat.format(currentDate);
        //判断当前是星期几
        if (currSun.equals(week)) {
            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 HH:mm:ss");
        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;
    }

    /**
     * 根据开始时间和结束时间计算之间的星期
     * @param beginDate
     * @param endDate
     * @return
     * @throws Exception
     */
    public static List<String> getWeekly(String beginDate,String endDate,String week) throws Exception{
        //获取俩个日期之间的日期
        List<String> list = findDates(beginDate, endDate);
        List<String> weekDateList = new ArrayList<>();
        //遍历
        for (String date : list) {
            //判断当前是星期几
            boolean thursday = weekdayOrNot(date,week);
            if (thursday==true) {
                weekDateList.add(date);
            }

        }
        return weekDateList;
    }

    public static List<String> findDates(String dBegin, String dEnd) throws ParseException {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        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;
    }

    /**
     * 日期转星期
     *
     * @param datetime
     * @return
     */
    public static String currentDataToWeek(String datetime) {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        // 获得一个日历
        Calendar cal = Calendar.getInstance();
        Date datet = null;
        try {
            datet = f.parse(datetime);
            cal.setTime(datet);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        // 指示一个星期中的某天。
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }
}

 

 

  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
可以使用Java的Calendar类来获取指定时间内的每个自然周的周一。具体方法如下: 1. 定义起始日期和结束日期。 2. 创建一个Calendar对象并设置为起始日期。 3. 使用while循环,判断当前日期是否在指定的时间内,如果是则继续。 4. 在循环内,先获取当前日期的星期几,如果是周日则往前推6天,如果是其他天则往前推相应天数,使其变成周一。 5. 输出当前的日期。 6. 将当前日期加7天,进入下一周的循环。 代码示例: ``` import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class GetMonday { public static void main(String[] args) { String fromDate = "2021-01-01"; // 起始日期 String toDate = "2021-02-28"; // 结束日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); try { // 设置起始日期 Date start = sdf.parse(fromDate); c.setTime(start); // 判断当前日期是否在指定时间内 while(c.getTime().before(sdf.parse(toDate)) || c.getTime().equals(sdf.parse(toDate))) { // 获取当前日期的星期几 int dayOfWeek = c.get(Calendar.DAY_OF_WEEK); // 将日期调整为当前周的周一 if(dayOfWeek == Calendar.SUNDAY) { c.add(Calendar.DAY_OF_MONTH, -6); } else { c.add(Calendar.DAY_OF_MONTH, (2 - dayOfWeek)); } // 输出当前日期 System.out.println(sdf.format(c.getTime())); // 将日期加7天,进入下一周 c.add(Calendar.DAY_OF_MONTH, 7); } } catch (Exception e) { e.printStackTrace(); } } } ``` 输出结果: ``` 2021-01-04 2021-01-11 2021-01-18 2021-01-25 2021-02-01 2021-02-08 2021-02-15 2021-02-22 ```
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值