| | |
| | | package com.hx.util; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Calendar; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获取两个时间相差分钟数 |
| | | * @param startTime 开始时间 |
| | | * @param endTime 结束时间 |
| | | * @param remainder 余数进1 |
| | | * @return 分钟 |
| | | */ |
| | | public static int differMinute(Date startTime, Date endTime,boolean remainder) { |
| | | BigDecimal sTime = new BigDecimal(startTime.getTime()); |
| | | BigDecimal eTime = new BigDecimal(endTime.getTime()); |
| | | eTime = eTime.subtract(sTime).setScale(0,RoundingMode.HALF_UP); |
| | | if(remainder){ |
| | | eTime = eTime.divide(BigDecimal.valueOf(60000.0)).setScale(0, RoundingMode.UP); |
| | | }else{ |
| | | eTime = eTime.divide(BigDecimal.valueOf(60000.0)).setScale(0, RoundingMode.DOWN); |
| | | } |
| | | return eTime.intValue(); |
| | | } |
| | | |
| | | /** |
| | | * endTime比startTime多的天数 |
| | | * @param startTime 最小时间 |
| | | * @param endTime 最大时间 |
| | |
| | | calendar.set(Calendar.HOUR_OF_DAY, 0); |
| | | calendar.set(Calendar.MINUTE, 0); |
| | | calendar.set(Calendar.SECOND, 0); |
| | | calendar.set(Calendar.MILLISECOND,0); |
| | | return calendar.getTime(); |
| | | } |
| | | |
| | |
| | | /** |
| | | * 获取当前月的结束时间 |
| | | * @param date 点前时间 |
| | | * @return 返回时间 格式yyyy-MM-dd 23:59:59 |
| | | * @return 返回时间 格式yyyy-MM-dd 23:59:59999 |
| | | */ |
| | | public static Date getMonthEnd(Date date) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); |
| | | calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); |
| | | calendar.set(Calendar.HOUR_OF_DAY, 23); |
| | | calendar.set(Calendar.MINUTE, 59); |
| | | calendar.set(Calendar.SECOND, 59); |
| | | calendar.set(Calendar.MILLISECOND,999); |
| | | return calendar.getTime(); |
| | | } |
| | | |
| | | /** |
| | | * 获取当前月的结束时间,没有毫秒 |
| | | * @param date 点前时间 |
| | | * @return 返回时间 格式yyyy-MM-dd 23:59:59 |
| | | */ |
| | | public static Date getMonthEndNoMillisecond(Date date) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); |
| | | calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); |
| | |
| | | calendar.set(Calendar.HOUR_OF_DAY, 0); |
| | | calendar.set(Calendar.MINUTE, 0); |
| | | calendar.set(Calendar.SECOND, 0); |
| | | calendar.set(Calendar.MILLISECOND,0); |
| | | return calendar.getTime(); |
| | | } |
| | | |
| | | /** |
| | | * 获取当前年的结束时间 |
| | | * @param time 时间 |
| | | * @return 返回时间 格式yyyy-MM-dd 00:00:00 |
| | | * @return 返回时间 格式yyyy-MM-dd 23:59:59999 |
| | | */ |
| | | public static Date getYearEnd(Date time) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(time); |
| | | 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); |
| | | calendar.set(Calendar.SECOND, 59); |
| | | calendar.set(Calendar.MILLISECOND,999); |
| | | return calendar.getTime(); |
| | | } |
| | | |
| | | /** |
| | | * 获取当前年的结束时间,没有毫秒 |
| | | * @param time 时间 |
| | | * @return 返回时间 格式yyyy-MM-dd 23:59:59 |
| | | */ |
| | | public static Date getYearEndNoMillisecond(Date time) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(time); |
| | | calendar.set(Calendar.MONTH, 11); |
| | |
| | | calendar.set(Calendar.HOUR_OF_DAY, 0); |
| | | calendar.set(Calendar.MINUTE, 0); |
| | | calendar.set(Calendar.SECOND, 0); |
| | | calendar.set(Calendar.MILLISECOND, 0); |
| | | return calendar.getTime(); |
| | | } |
| | | |
| | | /**这天的最后时间 |
| | | * 日期2000-01-01变2000-01-01 23:59:59 |
| | | * 日期2000-01-01变2000-01-01 23:59:59999 |
| | | */ |
| | | public static Date dayToEndDate(Date date) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); |
| | | calendar.set(Calendar.HOUR_OF_DAY, 23); |
| | | calendar.set(Calendar.MINUTE, 59); |
| | | calendar.set(Calendar.SECOND, 59); |
| | | calendar.set(Calendar.MILLISECOND, 999); |
| | | return calendar.getTime(); |
| | | } |
| | | |
| | | /**这天的最后时间,没有毫秒 |
| | | * 日期2000-01-01变2000-01-01 23:59:59 |
| | | */ |
| | | public static Date dayToEndDateNoMillisecond(Date date) { |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); |
| | | calendar.set(Calendar.HOUR_OF_DAY, 23); |
| | |
| | | } |
| | | return age; |
| | | } |
| | | |
| | | /** |
| | | * 获取某天结束秒数 |
| | | * @param dateTime 日期 |
| | | * @param lateSecond 延迟秒数 |
| | | * @return |
| | | */ |
| | | public static 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 static 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; |
| | | } |
| | | } |