java获取本周工时_js计算工时,去周末,设置上下班时间
时间: 2023-12-06 22:04:50 浏览: 87
以下是一个示例代码,可以用来计算本周工时:
```java
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class WeekWorkHour {
public static void main(String[] args) {
// 设置上班和下班时间
int startHour = 9;
int startMinute = 0;
int endHour = 18;
int endMinute = 0;
// 获取当前日期
Calendar cal = Calendar.getInstance();
Date now = cal.getTime();
// 获取本周的第一天和最后一天
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
String firstDayOfWeek = sdf.format(cal.getTime());
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
String lastDayOfWeek = sdf.format(cal.getTime());
// 计算工时
double totalWorkHour = 0;
for (int i = 0; i < 7; i++) {
cal.setTime(now);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY + i);
Date date = cal.getTime();
String dateString = sdf.format(date);
// 去掉周末
if (dateString.equals(firstDayOfWeek) || dateString.equals(lastDayOfWeek)) {
continue;
}
// 计算工作时间
double workHour = endHour - startHour;
if (endMinute > startMinute) {
workHour += (endMinute - startMinute) / 60.0;
} else {
workHour -= (startMinute - endMinute) / 60.0;
}
totalWorkHour += workHour;
}
System.out.println("本周工时:" + totalWorkHour);
}
}
```
这个示例代码假设上班时间是每天早上9点,下班时间是每天下午6点,可以根据实际情况进行修改。在计算工时时,会去掉周末(即周六和周日),只计算工作日的工时。
阅读全文