获取时间的下一天、周、月工具类

public class DateUtil {

    private final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    
    /**
     * 按传输时间返回时间的下一天
     * @param date 传输时间
     * @param day 天数左右移动,0-返回当前天
     * @return 下一天
     */
    public static String getNextDay(Date date, int day){  
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        calendar.roll(Calendar.DAY_OF_YEAR,day); 
        return sdf.format(calendar.getTime());  
     }
    
    /**
     * 按传输时间返回时间的下一月
     * @param date 传输时间
     * @param month 月数左右移动,0-返回当前月
     * @return 下一月
     */
    public static String getNextMonth(Date date, int month){  
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        calendar.roll(Calendar.MONTH,month); 
        return sdf.format(calendar.getTime());  
     }
    
    
    /**
     * 按传输判断是否工作日
     * @param date 传输时间
     * @return 是否工作日
     */
    public static boolean isWorkDate(Date date){  
        Calendar calendar = Calendar.getInstance(); 
        calendar.setTime(date);
        int day = calendar.get(Calendar.DAY_OF_WEEK);  
        if(!(day == Calendar.SUNDAY || day == Calendar.SATURDAY)){  
              return false;
        }  
        return true;  
     }
 
    /**
     * 按传输时间返回下一周的这一天
     * @param month 周数左右移动,0-返回当前周
     * @return 下一周
     */
    public static String getNextWeek(Date date, int week){  
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        calendar.roll(Calendar.WEEK_OF_YEAR,week); 
        return sdf.format(calendar.getTime());  
     }
    
    /**
     * 按传输时间返每周工作日,过去工作日不返回
     * @throws Exception 
     * @return 工作日
     */
    public static List<String> getWorkDay(Date date, int week) throws Exception{
        List<String> list =new ArrayList<String>();
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        calendar.roll(Calendar.WEEK_OF_YEAR,week);
        if(week ==0){
            int day = calendar.get(Calendar.DAY_OF_WEEK);  
            //是否工作日,是工作日的放入list集合放好
            while(!(day == Calendar.SUNDAY || day == Calendar.SATURDAY)){
                    list.add(sdf.format(calendar.getTime()));
                    calendar.roll(Calendar.DAY_OF_YEAR,1); 
                    day = calendar.get(Calendar.DAY_OF_WEEK);  
            }
        }else{
             //获取下周一时间
             calendar.roll(Calendar.WEEK_OF_YEAR,-1);
             int weeks = calendar.get(Calendar.DAY_OF_WEEK);
             if(weeks>2){
                calendar.add(Calendar.DAY_OF_MONTH,-(weeks-2)+7);
             }else{
                calendar.add(Calendar.DAY_OF_MONTH,2-weeks+7);
             }
             //获取下周一至五工作日
             list = getWorkDay(calendar.getTime(),0);
        }
        return list;  
    }
    
    
    public static void main(String[] args) throws Exception {
        //打印下一天
        System.out.println("下一天:"+getNextDay(new Date(),1));
        //打印下一月
        System.out.println("下一月:"+getNextMonth(new Date(),1));
        //打印是否工作日
        System.out.println("是否工作日:"+isWorkDate(new Date()));
        //打印下一周
        System.out.println("下一周:"+getNextWeek(new Date(),1));
        //打印每周工作日
        System.out.println("每周工作日:");
        List<String>  list = getWorkDay(new Date(),4);
        for(int i=0;i<list.size();i++){
            System.out.println(list.get(i).toString());
        }
    }

 

posted on 2013-08-04 00:32  阳光总在风雨后001  阅读(791)  评论(0编辑  收藏  举报

导航