| | |
| | | calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); |
| | | return calendar.getTime(); |
| | | } |
| | | |
| | | /** |
| | | * 根据出生年月日计算年龄 |
| | | * @param birth |
| | | * @return |
| | | */ |
| | | public static int getAge(Date birth) { |
| | | Calendar cal = Calendar.getInstance(); |
| | | int thisYear = cal.get(Calendar.YEAR); |
| | | int thisMonth = cal.get(Calendar.MONTH); |
| | | int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); |
| | | |
| | | cal.setTime(birth); |
| | | int birthYear = cal.get(Calendar.YEAR); |
| | | int birthMonth = cal.get(Calendar.MONTH); |
| | | int birthdayOfMonth = cal.get(Calendar.DAY_OF_MONTH); |
| | | |
| | | int age = thisYear - birthYear; |
| | | |
| | | // 未足月 |
| | | if (thisMonth <= birthMonth) { |
| | | // 当月 |
| | | if (thisMonth == birthMonth) { |
| | | // 未足日 |
| | | if (dayOfMonth < birthdayOfMonth) { |
| | | age--; |
| | | } |
| | | } else { |
| | | age--; |
| | | } |
| | | } |
| | | return age; |
| | | } |
| | | |
| | | /** |
| | | * 获取某天结束秒数 |
| | | * @param dateTime 日期 |
| | | * @param lateSecond 延迟秒数 |
| | | * @return |
| | | */ |
| | | public long todayEndSecond(Date dateTime, Long lateSecond) { |
| | | if(dateTime == null){ |
| | | dateTime = new Date(); |
| | | } |
| | | if(lateSecond == null){ |
| | | lateSecond = 0L; |
| | | } |
| | | Date endTime = DateUtil.dayToEndDate(dateTime); |
| | | return differSecond(dateTime, endTime) + lateSecond; |
| | | } |
| | | |
| | | /** |
| | | * 计算2个实际相差秒数 |
| | | * @param startTime 开始时间 |
| | | * @param endTime 结束时间 |
| | | * @return |
| | | */ |
| | | public long differSecond(Date startTime, Date endTime) { |
| | | if(startTime == null || endTime == null){ |
| | | return 0L; |
| | | } |
| | | long sTime = startTime.getTime(); |
| | | long eTime = endTime.getTime(); |
| | | return (eTime - sTime) / 1000L; |
| | | } |
| | | } |