获取指定月份的工作日天数
程序员文章站
2022-05-17 21:37:24
...
package com.gary.util;
import java.util.Calendar;
/**
* 日期工具
* @author gary
*
*/
public class DateUtil {
/**
* 计算两个日期的相隔天数
* @param d1
* @param d2
* @return
*/
public static int getDaysBetween(Calendar d1, Calendar d2) {
if (d1.after(d2)) {
Calendar swap = d1;
d1 = d2;
d2 = swap;
}
int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR);
int y2 = d2.get(Calendar.YEAR);
if (d1.get(Calendar.YEAR) != y2) {
d1 = (Calendar) d1.clone();
do {
days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);
d1.add(Calendar.YEAR, 1);
} while (d1.get(Calendar.YEAR) != y2);
}
return days;
}
/**
* 计算2个日期之间的工作日相隔天数
* @param d1
* @param d2
* @return
*/
public static int getWorkingDay(Calendar d1, Calendar d2) {
int result = -1;
if (d1.after(d2)) {
Calendar swap = d1;
d1 = d2;
d2 = swap;
}
//开始日期的日期偏移量
int charge_start_date = 0;
//结束日期的日期偏移量
int charge_end_date = 0;
// 日期不在同一个日期内
int stmp;
int etmp;
stmp = 7 - d1.get(Calendar.DAY_OF_WEEK);
etmp = 7 - d2.get(Calendar.DAY_OF_WEEK);
// 开始日期为星期六和星期日时偏移量为0
if (stmp != 0 && stmp != 6) {
charge_start_date = stmp - 1;
}
// 结束日期为星期六和星期日时偏移量为0
if (etmp != 0 && etmp != 6) {
charge_end_date = etmp - 1;
}
result = (getDaysBetween(getNextMonday(d1), getNextMonday(d2)) / 7)
* 5 + charge_start_date - charge_end_date;
return result + 1;
}
/**
* 获得日期的下一个星期一的日期
* @param date
* @return
*/
public static Calendar getNextMonday(Calendar date) {
Calendar result = null;
result = date;
do {
result = (Calendar) result.clone();
result.add(Calendar.DATE, 1);
} while (result.get(Calendar.DAY_OF_WEEK) != 2);
return result;
}
/**
* 获取指定年-月(yyyy-mm)的工作日天数
* @param year_month
* @return
*/
public static int getWorkingDayOfMonth(String year_month){
int year = Integer.parseInt(year_month.substring(0,year_month.indexOf("-")));
int month = Integer.parseInt(year_month.substring(year_month.indexOf("-") + 1));
Calendar start = Calendar.getInstance();
start.clear();
start.set(Calendar.YEAR, year);
start.set(Calendar.MONTH, month - 1);
start.set(Calendar.DAY_OF_MONTH, 1);
Calendar end = Calendar.getInstance();
end.clear();
end = (Calendar) start.clone();
end.add(Calendar.MONTH,1);
end.add(Calendar.DAY_OF_MONTH,-1);
return getWorkingDay(start,end);
}
/**
* Test
* @param args
*/
public static void main(String[] args) {
System.out.println(getWorkingDayOfMonth("2010-1"));
System.out.println(getWorkingDayOfMonth("2010-02"));
System.out.println(getWorkingDayOfMonth("2010-12"));
}
}