2013年10月6日 星期日

java-working-days daysInMonth-Holiday-Sunday-Saturday



static public String countWorkingDays(int year, int month) {
int count = 0;
int workingDays=0;
String result="";
try {
ResultSet rs = null;
Connection con = null;
Statement stmt = null;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/hr?useUnicode=true&characterEncoding=UTF8","root", "root");
stmt = con.createStatement();
Calendar calendar = Calendar.getInstance();
// Note that month is 0-based in calendar, bizarrely.
calendar.set(year, month - 1, 1);
int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
//result=result+ "\n year "+Integer.toString(year);
//result=result+" mth "+Integer.toString(month)+"\n";
for (int day = 1; day <= daysInMonth; day++) {
String s=Integer.toString(year);
String s1=Integer.toString(month);
String s2=Integer.toString(day);
String sqlString="select count(*) from holiday  where date='"+s+"-"+s1+"-"+s2+"';";
rs = stmt.executeQuery(sqlString);
rs.next();
int k=rs.getInt(1);
if (k==1)
{
count++;
}
calendar.set(year, month - 1, day);
int i= calendar.get(Calendar.DAY_OF_WEEK);
if (i== Calendar.SUNDAY || i == Calendar.SATURDAY) {
count++;
}
}
workingDays=daysInMonth-count;
} catch (Exception e) {e.printStackTrace();result=result+"\n"+e+"\n";}
result=result+Integer.toString(workingDays);
return result;
}

沒有留言:

張貼留言