您好,登錄后才能下訂單哦!
在Java中使用Calendar類來計算工作日(即排除周末)可以通過以下方法實現:
以下是一個簡單的示例代碼來計算指定日期范圍內的工作日數量:
import java.util.Calendar;
public class WorkdayCalculator {
public static void main(String[] args) {
Calendar start = Calendar.getInstance();
start.set(2022, Calendar.JANUARY, 1);
Calendar end = Calendar.getInstance();
end.set(2022, Calendar.DECEMBER, 31);
int workdays = calculateWorkdays(start, end);
System.out.println("Number of workdays: " + workdays);
}
public static int calculateWorkdays(Calendar start, Calendar end) {
int workdays = 0;
Calendar current = (Calendar) start.clone();
while (current.before(end) || current.equals(end)) {
if (current.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY && current.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
workdays++;
}
current.add(Calendar.DAY_OF_MONTH, 1);
}
return workdays;
}
}
性能優化技巧:
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。