java 節假日與週末重疊天數
阿新 • • 發佈:2018-12-19
筆記
import java.text.SimpleDateFormat;
import java.util.Calendar;
/**
* 節假日與週末重疊 天數
* @param startHoliday 假期開始時間
* @param holiday 假期天數
* @param restType 休息方式 1:雙休 2:週日單休
* @return
* @throws Exception
*/
private long isReapert(String startHoliday,long holiday,long restType) throws Exception{
long reatpertDay =0;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
int dayForWeek = 0;
if(holiday==1){//假期只有一天,且為雙休形式,得出一天假期為星期幾
Calendar c = Calendar.getInstance();
c.setTime(format.parse(startHoliday));
if (c.get(Calendar.DAY_OF_WEEK) == 1){
dayForWeek = 7;
}else{
dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
}
if(restType==1){
if(dayForWeek==6||dayForWeek==7){
reatpertDay = 1;
}
}
if (restType==2){
if(dayForWeek==7){
reatpertDay = 1;
}
}
}else{
Calendar calendar = new GregorianCalendar();
calendar.setTime(format.parse(startHoliday));
for(int i=0;i<holiday;i++){
calendar.add(calendar.DATE, i);
if(calendar.get(Calendar.DAY_OF_WEEK) == 1){
dayForWeek = 7;
}else{
dayForWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
}
if(restType==1){
if(dayForWeek==6||dayForWeek==7){
reatpertDay++;
}
}
if(restType==2){
if(dayForWeek==7){
reatpertDay++;
}
}
}
}
return reatpertDay;
}
要根據專案實際情況而定