| | |
| | | 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.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); |
| | | calendar.set(Calendar.MINUTE, 59); |
| | | calendar.set(Calendar.SECOND, 59); |
| | | return calendar.getTime(); |
| | | } |
| | | |