java中计算工作日
程序员文章站
2022-05-17 21:06:00
...
/**
* 计算工作日 如果两个日期之间包含节假日,就除去节假日
*
* @param conn
* Connection
* @param start
* @param end
* @return int
* @throws StoreException
* @throws SQLException
* eg.
* int i = DateTools.countWorkDay(this.getJdbcTemplate().getDataSource().getConnection(),
new SimpleDateFormat("yyyy-MM-dd").parse("2013-11-01"), new SimpleDateFormat("yyyy-MM-dd").parse("2013-11-04"));
*
*/
public static int countWorkDay(Connection conn, Date start, Date end) throws SQLException
{
// 工作日
int workDay = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
int thisyear = start.getYear() + 1900;
int nextyear = thisyear == end.getYear() ? thisyear + 1 : end.getYear() + 1900;
PreparedStatement stmt = null;
ResultSet rset = null;
String sql = "select date(t.ATDAY) from bas_set_holidays t where t.ATYEAR = ? or t.ATYEAR = ?";// 这个表手动维护,由国务院发布放假通知后添加
stmt = conn.prepareStatement(sql);
stmt.setString(1, String.valueOf(thisyear));
stmt.setString(2, String.valueOf(nextyear));
rset = stmt.executeQuery();
// 得到今年和明年的所有的假日
String holidays = "";
while (rset.next())
{
holidays += rset.getString(1) + ",";
}
int days = DateTools.getDiffDays(start, end);
Timestamp tempDate;
for (int i = 0; i <= days; i++)
{
tempDate = new Timestamp(start.getTime() + i * 86400000);
if (holidays.indexOf(sdf.format((Date) tempDate)) == -1)
{
workDay += 1;
}
}
return workDay;
}
CREATE TABLE "BAS_SET_HOLIDAYS" (
"RECID" TEXT(32) NOT NULL,
"ATDAY" TEXT,
"ATYEAR" TEXT,
"WEEKDAY" TEXT,
"TYPE" TEXT,
PRIMARY KEY ("RECID")
);
上一篇: java8获取一个时间段内的所有年月日期
下一篇: 工作日计算学习