JAVA给定两个日期判断是第几周(中…

public static String getWeekCounterByEventStartDate(String event_s_date, String dailyDate) {
        if (isSameWeek(event_s_date, dailyDate)) {
            return "WK1";
        }
        Calendar c_base = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        Calendar tempC = Calendar.getInstance();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        int week = 0;
        try {
            c_base.setTime(df.parse(event_s_date));
            c2.setTime(df.parse(dailyDate));
            Long time1 = c2.getTimeInMillis();
            Long time2 = c_base.getTimeInMillis();
            Long dayDiffer = Math.abs(time1 - time2) / (1000 * 60 * 60 * 24);//毫秒*秒*分*小时
            if (dayDiffer.intValue() == 0) {
                week++;
                return "WK" + week;
            }
            int curTodayWeek = c_base.get(Calendar.DAY_OF_WEEK) - 1;
            int differIsWeek = 7 - curTodayWeek;
            tempC = (Calendar) c_base.clone();

            if (differIsWeek != 0) {
                week++;
            }
            if (differIsWeek != 7) {
                tempC.add(Calendar.DAY_OF_YEAR, differIsWeek);
            }
            time1 = c2.getTimeInMillis();
            time2 = tempC.getTimeInMillis();
            dayDiffer = Math.abs(time1 - time2) / (1000 * 60 * 60 * 24);//毫秒*秒*分*小时
            if (dayDiffer.intValue() == 1) {//差一天
                week++;
            } else if (dayDiffer.intValue() < 7) {
                week++;
            } else {
                DecimalFormat decimalFormat = new DecimalFormat("#.0");
                String dayNum = decimalFormat.format((double) dayDiffer.intValue() / 7);
                int day = Integer.parseInt(dayNum.substring(0, dayNum.indexOf(".")));
                week += day;
                int dec = Integer.parseInt(dayNum.substring(dayNum.indexOf(".") + 1, dayNum.length()));
                if (dec != 0) {
                    week++;
                }
            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            return "";
        }
        return "WK" + week;
    }

    //第一种  isSameWeek

    public static boolean isSameWeek(String date1, String date2) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date d1 = null;
        Date d2 = null;
        try {
            d1 = format.parse(date1);
            d2 = format.parse(date2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        cal1.setTime(d1);
        cal2.setTime(d2);

        int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
        int subMonth = (cal1.get(Calendar.MONTH) + 1) - (cal2.get(Calendar.MONTH) + 1);

        //同一年,同一月
        if (subYear == 0 && subMonth == 0) {
            int week1 = cal1.get(Calendar.WEEK_OF_MONTH);
            int week2 = cal2.get(Calendar.WEEK_OF_MONTH);
            int wd1 = cal1.get(Calendar.DAY_OF_WEEK);
            int wd2 = cal2.get(Calendar.DAY_OF_WEEK);
            if (wd1 == 1) {
                week1 -= 1;
            }
            if (wd2 == 1) {
                week2 -= 1;
            }
            return week1 == week2;
        }
        return false;
    }

    public static void main(String[] args) {
        System.out.println(getWeekCounterByEventStartDate("2013-8-1", "2013-8-10"));
    }

 

   //第二种  isSameWeek

public static void main(String[] args) throws Exception {
        DateFormat df = new SimpleDateFormat("yyyy-M-d");
        System.out.println(isSameWeek(df.parse("2013-10-11"), df.parse("2013-10-12")));
        System.out.println(isSameWeek(df.parse("2013-8-1"), df.parse("2013-8-5")));
    }

    public static boolean isSameWeek(Date d1, Date d2) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyww");//年周
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(Calendar.MONDAY);//Calendar.DAY_OF_WEEK 是不被改变的
        format.setCalendar(cal);
        return format.format(d1).equals(format.format(d2));//format.format(d1) 年+一年的第几周字符串例如 2013-8-1 是一年的第31周
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值