java时间计算,获取某月第一天和最后一天

        //获取前月的第一天
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); 
        //获取当前月第一天:
        Calendar c = Calendar.getInstance();

        String first = null;//第一天
        String last = null;//第二天
        //判断获取的参数如果为-1
        if(month.equals("-1")){
            c.add(Calendar.MONTH, -1);
            c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
            first = sdf.format(c.getTime());

            //获取上月的最后一天
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_MONTH, 1); 
            calendar.add(Calendar.DATE, -1);
            last = sdf.format(calendar.getTime());
        }else{
            c.add(Calendar.MONTH, 0);
            c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
            first = sdf.format(c.getTime());

            //获取当前月最后一天
            Calendar ca = Calendar.getInstance();    
            ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));  
            last = sdf.format(ca.getTime());
        }


        //循环时间段
        Date startDate = sdf.parse(first);
    
        Date endDate = sdf.parse(last);

        Calendar start = Calendar.getInstance();
        start.setTime(startDate);
        Long statTime = start.getTimeInMillis();
        
        Calendar end = Calendar.getInstance();
        end.setTime(endDate);
        Long endTime = end.getTimeInMillis();

        Long oneDay = 1000 * 60 * 60 * 24l; 

        Long time = statTime;  

        List timeList=new ArrayList();
        while (time <= endTime) {  
            Date d = new Date(time);
            DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
            time += oneDay;
            timeList.add(df.format(d));
        }  

        //根据渠道ID查询当前代理公司下有多少交易成功的单
        
        String lastDate = null;
        if(month.equals("-1")){
            //获取这个时间段下一天
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_MONTH, 1); 
            calendar.add(Calendar.DATE, 0);
            lastDate = sdf.format(calendar.getTime());
        }else{
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            calendar.add(Calendar.MONTH, 1);
            lastDate = sdf.format(calendar.getTime());
        }

这是获取时间,然后这个时间进行相加

比如

updateDate  = "2016-11-9"
//得到保单状态最后修改的时间
Date updateDate = tranCar.getDtUpdateDate();
//计算时间,+3天
Calendar calendar = Calendar.getInstance();
calendar.setTime(updateDate);
calendar.add(calendar.DATE,3);
//得出的结果
updateDate  = "2016-11-12"

//获取上个月25号的时间
    private String setDayLastMonth(SimpleDateFormat format) {
        Calendar month = Calendar.getInstance();
        month.add(Calendar.MONTH,-1);
        month.set(Calendar.DATE,25);
        String oneDayLastMonth = format.format(month.getTime());
        return oneDayLastMonth;
    }


//获取当月的时间,这里时间不能超过当月25号
    private String setDayBefore(SimpleDateFormat format) {
        Calendar cal = Calendar.getInstance();//获取一个日历
        cal.add(Calendar.DAY_OF_MONTH, -1);//取当前日期的前一天.

        //当前时间
        String today = format.format(Calendar.getInstance().getTime());
        String theDayBefore = format.format(cal.getTime());
        //比较时间大小
        Date theDayBeforeDate = cal.getTime();//当前时间的前一天日期格式
        Date todayDate = Calendar.getInstance().getTime();//当前时间日期格式

        if(theDayBeforeDate.getTime() > todayDate.getTime()){
            //默认日期只能为本月的24日
            cal.set(Calendar.DATE,24);
            theDayBefore = format.format(cal.getTime());
        }
        return theDayBefore;
    }

 

posted @ 2016-11-09 19:33  夜天晰  阅读(13485)  评论(0编辑  收藏  举报