计算周、月的开始与结束日期


       
        SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd , ww, E");
        //sdf.format("2009-12-31");
        Calendar cl = Calendar.getInstance();
        cl.setFirstDayOfWeek(GregorianCalendar.SUNDAY);// 每周以周日开始
        cl.setMinimalDaysInFirstWeek(3);                // 每年的第一周必须大于或等于3天,否则就算上一年的最后一周
       
//        cl.set(2009, 11, 26);    // 使用SimpleDateFormat获取的周数是错误的,get(Calendar.WEEK_OF_YEAR)是对的
//        System.out.println(sdf.format(cl.getTime())+"/t"+cl.get(Calendar.WEEK_OF_YEAR)+"/t"+getYearOfWeek(cl));
//        cl.set(2009, 11, 27);
//        System.out.println(sdf.format(cl.getTime())+"/t"+cl.get(Calendar.WEEK_OF_YEAR)+"/t"+getYearOfWeek(cl));
//        cl.set(2010, 0, 1);
//        System.out.println(sdf.format(cl.getTime())+"/t"+cl.get(Calendar.WEEK_OF_YEAR)+"/t"+getYearOfWeek(cl));
//        cl.set(2010, 0, 2);
//        System.out.println(sdf.format(cl.getTime())+"/t"+cl.get(Calendar.WEEK_OF_YEAR)+"/t"+getYearOfWeek(cl));
       
       
        sdf =  new SimpleDateFormat("yyyy-MM-dd");
        cl.set(2010,4-1,25);    // 2010-04-25
        System.out.println(sdf.format(cl.getTime())+" 属于第"+cl.get(Calendar.WEEK_OF_YEAR)+"周");
        cl.set(Calendar.DAY_OF_WEEK,GregorianCalendar.SUNDAY);
        System.out.println("第"+cl.get(Calendar.WEEK_OF_YEAR)+"周第1天:"+sdf.format(cl.getTime()));
        cl.roll(Calendar.DAY_OF_WEEK,-1);
        System.out.println("第"+cl.get(Calendar.WEEK_OF_YEAR)+"周第7天:"+sdf.format(cl.getTime()));
       
       
        cl.set(2010,4-1,25);
        cl.add(Calendar.WEEK_OF_YEAR, -1);
        System.out.println(sdf.format(cl.getTime())+" 属于第"+cl.get(Calendar.WEEK_OF_YEAR)+"周");
        cl.set(Calendar.DAY_OF_WEEK,GregorianCalendar.SUNDAY);
        System.out.println("第"+cl.get(Calendar.WEEK_OF_YEAR)+"周第1天:"+sdf.format(cl.getTime()));
        cl.roll(Calendar.DAY_OF_WEEK,-1);
        System.out.println("第"+cl.get(Calendar.WEEK_OF_YEAR)+"周第7天:"+sdf.format(cl.getTime()));
       
        System.out.println("==================");
       
        cl.set(2010,4-1,25);    // 2010-04-25
        System.out.println(sdf.format(cl.getTime())+" 属于第"+(cl.get(Calendar.MONTH)+1)+"月");
        cl.set(Calendar.DAY_OF_MONTH, 1);
        System.out.println(sdf.format(cl.getTime()));
        cl.roll(Calendar.DAY_OF_MONTH, -1);
        System.out.println(sdf.format(cl.getTime()));
       
        cl.set(2010,4-1,25);    // 2010-04-25
        cl.add(Calendar.MONTH, -1);
        System.out.println(sdf.format(cl.getTime())+" 属于第"+(cl.get(Calendar.MONTH)+1)+"月");
        cl.set(Calendar.DAY_OF_MONTH, 1);
        System.out.println(sdf.format(cl.getTime()));
        cl.roll(Calendar.DAY_OF_MONTH, -1);
        System.out.println(sdf.format(cl.getTime()));
       

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值