| | |
| | | * endTime比startTime多的天数 |
| | | * @param startTime 最小时间 |
| | | * @param endTime 最大时间 |
| | | * @return |
| | | * @return 返回 |
| | | */ |
| | | public static Integer differDay(Date startTime,Date endTime){ |
| | | Calendar cal1 = Calendar.getInstance(); |
| | |
| | | } |
| | | |
| | | /**把秒转换成X天X时X分X秒*/ |
| | | public static String getChineseStr(Integer second) |
| | | { |
| | | public static String getChineseStr(Integer second) { |
| | | int day = 24 * 60 * 60; |
| | | int hour = 60 * 60; |
| | | int min = 60; |
| | |
| | | public static Date getYearStart(Date time) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(time); |
| | | calendar.set(Calendar.MONTH, 1); |
| | | calendar.set(Calendar.MONTH, 0); |
| | | calendar.set(Calendar.DAY_OF_MONTH, 1); |
| | | calendar.set(Calendar.HOUR_OF_DAY, 0); |
| | | calendar.set(Calendar.MINUTE, 0); |
| | |
| | | public static Date getYearEnd(Date time) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(time); |
| | | calendar.set(Calendar.MONTH, 12); |
| | | calendar.set(Calendar.MONTH, 11); |
| | | calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DATE)); |
| | | calendar.set(Calendar.HOUR_OF_DAY, 23); |
| | | calendar.set(Calendar.MINUTE, 59); |
| | |
| | | return sdf.format(calendar.getTime()); |
| | | } |
| | | |
| | | /**这天的开始时间 |
| | | * 日期2000-01-01变2000-01-01 00:00:00 |
| | | */ |
| | | public static Date dayToStartDate(Date date) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); |
| | | calendar.set(Calendar.HOUR_OF_DAY, 0); |
| | | calendar.set(Calendar.MINUTE, 0); |
| | | calendar.set(Calendar.SECOND, 0); |
| | | return calendar.getTime(); |
| | | } |
| | | |
| | | /**这天的最后时间 |
| | | * 日期2000-01-01变2000-01-01 23:59:59 |
| | | */ |
| | |
| | | cal.roll(Calendar.DATE, -1); |
| | | return cal.getActualMaximum(Calendar.DATE); |
| | | } |
| | | |
| | | /**获取月份的天数 |
| | | * @param year 年份 |
| | | * @param month 月份 |
| | |
| | | /**获取月份的天数 |
| | | * @param yearMonth 年月 |
| | | * @param format 时间格式 |
| | | * @return |
| | | * @return 返回 |
| | | */ |
| | | public static int getMonthDays(String yearMonth,String format) { |
| | | SimpleDateFormat sdf = new SimpleDateFormat(format); |
| | |
| | | month.roll(Calendar.DATE, -1); |
| | | return month.getActualMaximum(Calendar.DATE) - Calendar.getInstance().get(Calendar.DAY_OF_MONTH); |
| | | } |
| | | |
| | | /**获取当前时间所在周的周一00:00:00*/ |
| | | public static Date getMonday(Date date) { |
| | | Calendar calendar = Calendar.getInstance(Locale.CHINA); |
| | | calendar.setTime(date); |
| | | //以周一为首日 |
| | | calendar.setFirstDayOfWeek(Calendar.MONDAY); |
| | | calendar.set(Calendar.HOUR_OF_DAY, 0); |
| | | calendar.set(Calendar.MINUTE, 0); |
| | | calendar.set(Calendar.SECOND, 0); |
| | | //周一 |
| | | 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; |
| | | } |
| | | } |