直接上代码,不多说。可以封装成公用类使用。
package com.edutohome.service;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import com.edutohome.dao.studentinfoDaoImpl;
import com.edutohome.vo.Attendance;
public class GetUnWeekendDays {
//返回截止目前为止,当月工作日数
public int getAttendance() throws Exception{
int count = 0;
int UnWeek;
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String NowDate = df.format(date);
String Days = NowDate.substring(8, 10);
String item = Days.substring(0, 1);
if(item.equals("0")){
Days = Days.substring(1,2);
}
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
String strDate = NowDate.substring(0,10);
//String strDate = "2016-10-10"; //test by Jiro.Chen
String item1 = strDate.substring(0, 8);
String item2 = strDate.substring(8,10);
for(int i= 1; i <= Integer.parseInt(item2); i++){
strDate = item1 + String.valueOf(i);
Date dDate = format1.parse(strDate);
Calendar cal = Calendar.getInstance();
cal.setTime(dDate);
if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
//System.out.println("YES");
count++;
}else{
//System.out.println("NO");
continue;
}
}
UnWeek = Integer.parseInt(item2) - count;
return UnWeek;
}
public static void main(String args[]) throws Exception{
SelectService sel = new SelectService();
System.out.println(sel.getAttendance());
}
}