我有两个日期,2013年11月19日招聘和2014年1月10日结束招聘,都转换为总工时,不考虑周末,但他们有不同的年份,正因为如此,输出显示:总工时为-1200:
private int calculateTimeInternship(Vacancy peoplevacancy){
int hourWorked = 0;
Calendar date1 = Calendar.getInstance();
Calendar date2 = Calendar.getInstance();
date1.setTime(peoplevacancy.getDthiring());
date2.setTime(peoplevacancy.getDtendhiring());
int initiation = date1.get(Calendar.DAY_OF_YEAR);
int end = date2.get(Calendar.DAY_OF_YEAR);
int amountDay = (initiation - end) + 1;
for (; initiation <= end; inicio++){
if (date1.get(Calendar.DAY_OF_WEEK) == 1 || date1.get(Calendar.DAY_OF_WEEK) == 7)
amountDay--;
date1.add(Calendar.DATE, 1);
}
hourWorked = amountDay * 4 //4 hour per day;
return hourWorked ;
}
最佳答案
Joda可以帮到你,但因为它的许可证,我永远无法使用它。
如果像我一样,乔达不适合你,你可以解决这个问题如下:
initialize endDate object
initialize startDate object
initialize weeksBetween as
milliseconds between end&start/milliseconds per day, divided by seven (integer floor).
//may need to normalize dates and set them to be both midnight or noon or some common time
initialize daysBetween = weeksBetween*5 // in any continuous 7 days, 5 are weekdays.
initialize curDay=startDate + weeksBetween*7 days
while(curDay is not endDate)
add a day to curDay
if(curDay is not weekend)
daysBetween++
output daysBetween* 4
您可以通过将日历转换为Date来获得它们之间的毫秒数(Calendar有这样一个方法可以做到这一点)