java計算工齡


計算工齡原則:若是2000-10-12作為開始工作時間,則到下一年的2001-10-13算為一年。有個bug,不滿一年的工齡是錯誤的。

import java.util.Date;
import java.util.Calendar;

public int workAge(Date nowTime, Date workTime){
int year = 0;
//當前時間的年月日
Calendar cal = Calendar.getInstance();
cal.setTime(nowTime);
int nowYear = cal.get(Calendar.YEAR);
int nowMonth = cal.get(Calendar.MONTH);
int nowDay = cal.get(Calendar.DAY_OF_MONTH);

//開始工作時間的年月日
cal.setTime(workTime);
int workYear = cal.get(Calendar.YEAR);
int workMonth = cal.get(Calendar.MONTH);
int workDay = cal.get(Calendar.DAY_OF_MONTH);

//得到工齡
year = nowYear - workYear; //得到年差
//若目前月數少於開始工作時間的月數,年差-1
if (nowMonth < workMonth){
year = year - 1;
}else if (nowMonth == workMonth){
//當月數相等時,判斷日數,若當月的日數小於開始工作時間的日數,年差-1
if (nowDay < workDay){
year = year - 1;
}
}

return year;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM