java 获取十个工作日之前或之后的日期(算当天)

/**
     *
     * 根据开始日期 ,需要的工作日天数 ,计算工作截止日期,并返回截止日期
     * @param startDate 开始日期
     * @param workDay 工作日天数(周一到周五)
     */
    public static String getWorkDay(Date startDate, int workDay) {
        Calendar c1 = Calendar.getInstance();
        c1.setTime(startDate);
        for (int i = 0; i < workDay; i++) {
            c1.set(Calendar.DATE, c1.get(Calendar.DATE) - 1);
            if (Calendar.SATURDAY == c1.get(Calendar.SATURDAY) || Calendar.SUNDAY == c1.get(Calendar.SUNDAY)) {
                workDay = workDay + 1;
                c1.set(Calendar.DATE, c1.get(Calendar.DATE) - 1);
                continue;
            }
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        c1.set(Calendar.DATE, c1.get(Calendar.DATE) +1);
        return df.format(c1.getTime());
    }

    /**
     *
     * 根据开始日期 ,需要的工作日天数 ,计算工作截止日期,并返回截止日期
     * @param startDate 开始日期
     * @param workDay 工作日天数(周一到周五)
     */
    public static Date getWorkDay(Date startDate, int workDay) {
        Calendar c1 = Calendar.getInstance();
        c1.setTime(startDate);
        for (int i = 0; i < workDay; i++) {
            c1.set(Calendar.DATE, c1.get(Calendar.DATE) + 1);
            if (Calendar.SATURDAY == c1.get(Calendar.SATURDAY) || Calendar.SUNDAY == c1.get(Calendar.SUNDAY)) {
                workDay = workDay + 1;
                c1.set(Calendar.DATE, c1.get(Calendar.DATE) + 1);
                continue;
            }
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(df.format(c1.getTime()) + " " + getWeekOfDate(c1.getTime()));
        return c1.getTime();
    }
    /**
     * 根据日期,获取星期几
     * @param dt
     * @return String类型
     */
    public static String getWeekOfDate(Date dt) {
        String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return weekDays[w];
    }

转自:https://www.cnblogs.com/feiyuanxing/p/4990542.html

posted @ 2021-08-02 17:53  ToDarcy  阅读(1083)  评论(0编辑  收藏  举报