工作日计算 去掉周末
随便写了个class,比较嗦,可以用
/* *@(#)WorkDay.java *Copyright (c) 2007-2008 *All right reserved */package time;import java.text.SimpleDateFormat;import java.util.*;/** *Class description goes here * *@author Bug Liu *@version ewms 2.0 *@time 2011-7-6 *@description */public class WorkDay {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubSystem.out.println(getDay("2011-07-01","2011-07-01"));}/** * * @param time1 开始时间 * @param time2结束时间 * @return */public static int getDay(String time1, String time2) {int day = 0;if(time1==time2){if(!ifWeek(time1)){day = 0;}else{day = 1 ;}}else{String nexttime = getNextDatetostring(time1);while(!CompareToDate(nexttime,time2)){if(!ifWeek(nexttime)){day++;}nexttime = getNextDatetostring(nexttime);}}return day;}/** * 是否周末 * @param str * @return */public static boolean ifWeek(String str){if(getWeekOfDay(str)==6||getWeekOfDay(str)==7){return true;}else{return false ;}}/** * 计算星期几 * @param str * @return */public static int getWeekOfDay(String str) {String[] arr = str.split("-");Calendar cal = Calendar.getInstance();cal.set(Integer.parseInt(arr[0]), Integer.parseInt(arr[1]) - 1, Integer.parseInt(arr[2]));return cal.get(Calendar.DAY_OF_WEEK);}/** * @return 系统给定日期的下一天 */public static String getNextDatetostring(String date) {java.util.Date now = getDateByString(date);SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");String xtsj = sdf.format(dateAdd(now,1,0)); return xtsj;}/** * 返回给定的日期在指定时间段后的日期,指定con为0时为天,1-月,2-年 * Description * * @param d 日期 * @param x 给定数目 * @param con给定操作 * @return */public static Date dateAdd(Date date, int x, int con) {Calendar cal = Calendar.getInstance();cal.setTime(date);if (con == 0){int d = cal.get(Calendar.DAY_OF_MONTH); d = d + x; cal.set(Calendar.DAY_OF_MONTH, d); }if (con == 1){int m= cal.get(Calendar.MONTH);m= m+x;cal.set(Calendar.MONTH, m);}if (con == 2){int y = cal.get(Calendar.YEAR);y = y +x;cal.set(Calendar.YEAR, y);}return cal.getTime();}/** * 根据给定的字符串生成日期,字符串的格式为yyyy-mm-dd * Description * * @param str * @return */public static Date getDateByString(String str) {String[] arr = str.split("-");Calendar cal = Calendar.getInstance();cal.set(Integer.parseInt(arr[0]), Integer.parseInt(arr[1]) - 1, Integer.parseInt(arr[2]));return cal.getTime();}/**@description 比较日期大小 * * @param date_1 * @param date_2 * @return 前面日期大,返回true */public static boolean CompareToDate(String date_1,String date_2){boolean temp = false ; int result = date_1.compareTo(date_2); if (result > 0) { temp = true; }else{ temp = false; }return temp ;}}