package com.yanzuoguang.util.helper; import com.yanzuoguang.util.exception.ExceptionHelper; import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * 日期工具类 * * @author 颜佐光 */ public class DateHelper { public static final String FORMAT_YEAR_STRING = "yyyy-01-01"; public static final String FORMAT_YEAR_END_STRING = "yyyy-12-31"; public static final String FORMAT_MONTH_STRING = "yyyy-MM-01"; public static final String FORMAT_DAY_STRING = "yyyy-MM-dd"; public static final String FORMAT_TIME_STRING = "HH:mm:ss"; public static final String INIT_TIME = "00:00:00"; public static final String FORMAT_DAY_HOUR_STRING = "yyyy-MM-dd HH:00:00"; public static final String FORMAT_SECOND_STRING = "yyyy-MM-dd HH:mm:ss"; public static final int INIT_MILL_SECOND_NONE = 0; public static final int INIT_MILL_SECOND_START = 1; public static final int INIT_MILL_SECOND_END = 2; private static final int MONTH_1 = 1; private static final int MONTH_2 = 2; private static final int MONTH_3 = 3; private static final int MONTH_4 = 4; private static final int MONTH_5 = 5; private static final int MONTH_6 = 6; private static final int MONTH_7 = 7; private static final int MONTH_8 = 8; private static final int MONTH_9 = 9; private static final int MONTH_10 = 10; private static final int MONTH_11 = 11; private static final int MONTH_12 = 12; /** * 判断时间是否是初始化时间 * * @param time 历史初始化日期 * @return 新的日期 */ public static final boolean isInitTime(String time) { if (StringHelper.isEmpty(time)) { return true; } return StringHelper.compare(time, INIT_TIME.substring(0, time.length())); } /** * 判断日期是否需要初始化,当需要时,返回新的日期 * * @param initDay 历史初始化日期 * @param content 新的日期 * @return 新的日期 */ public static final String getInitDay(String initDay, String content) { String today = DateHelper.getToday(); String contentDay = DateHelper.getToday(content); if (!StringHelper.isEmpty(initDay)) { initDay = DateHelper.getToday(initDay); if (StringHelper.compare(contentDay, initDay)) { return StringHelper.EMPTY; } } if (!StringHelper.compare(contentDay, today)) { return StringHelper.EMPTY; } return today; } /** * 获取时间 * * @param from 来源时间 * @param initMillSecond 是否初始化毫秒为0 * @return 结束时间 */ public static Date getDateTime(Object from, int initMillSecond) { if (StringHelper.isEmpty(from)) { return null; } try { Date to; if (from instanceof Date) { to = (Date) from; } else { to = DateAutoHelper.getAutoDate(String.valueOf(from)); } if (initMillSecond == INIT_MILL_SECOND_NONE) { return to; } return initMillSecond(to, initMillSecond); } catch (Exception ex) { ExceptionHelper.handleException(DateHelper.class, ex, from); } return null; } /** * 组合日期和时间 * * @param today 日期 * @param time 时间 * @return today(日期)+时间 */ public static Date mergeTodayTime(Object today, String time) { if (StringHelper.isEmpty(today)) { return null; } time = StringHelper.getFirst(time, "00:00:00"); String day = DateHelper.getToday(today); String toResult = String.format("%s %s", day, time); return DateHelper.getDateTime(toResult); } /** * 获取时间,会自动初始化毫秒为0 * * @param from 来源时间 * @return 结束时间 */ public static Date getDateTime(Object from) { return getDateTime(from, INIT_MILL_SECOND_START); } /** * Timestamp 转成 "yyyy-MM-dd HH:mm:ss"字符串 * * @param date 需要处理的日期 * @return 转换后的字符串 */ public static String toString(Timestamp date) { if (date == null) { return StringHelper.EMPTY; } SimpleDateFormat format = new SimpleDateFormat(FORMAT_SECOND_STRING); return format.format(date); } /** * 取得当前日期对象 * * @return 返回java.util.Date日期对象 */ public static Date getCurDate() { return new Date(); } /** * 将指定Date类型转换成指定格式的字符串,格式串参见类注释 * * @param date 日期方式 * @param format 指定的格式,当format为NULL或空串时,<BR> * 默认为 yyyy-MM-dd 格式 * @return 当date为NULL时, 返回空串 */ public static String getDate(Date date, String format) { if (date == null) { return StringHelper.EMPTY; } if (format == null || format.length() == 0) { format = "yyyy-MM-dd"; } SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(date); } /** * 获取日期 yyyy-MM-dd * * @param date 日期参数 * @return 返回的结果 */ public static String getYearMonthDay(Date date) { return getDate(date, FORMAT_DAY_STRING); } /** * 取得当前日期的字符串表示,格式为 yyyy-MM-dd * * @return 返回日期的字符串表示 */ public static String getToday() { return getYearMonthDay(getCurDate()); } /** * 取得当前日期的字符串表示,格式为 yyyy-MM-dd * * @param date * @return */ public static String getToday(Object date) { return getYearMonthDay(DateHelper.getDateTime(date)); } /** * 取得当前日期的字符串表示,格式为 yyyy-MM-dd * * @param date * @return */ public static String getToday(Date date) { return getYearMonthDay(date); } /** * 获取日期 yyyy-MM-dd HH:00:00 * * @param date 日期参数 * @return 返回的结果 */ public static String getYearMonthDayHour(Date date) { return getDate(date, FORMAT_DAY_HOUR_STRING); } /** * 取得当前日期的字符串表示,格式为 yyyy-MM-dd HH:00:00 * * @return 返回日期的字符串表示 */ public static String getTodayHour() { return getYearMonthDayHour(getCurDate()); } /** * 取得当前日期的字符串表示,格式为 yyyy-MM-dd HH:00:00 * * @param date * @return */ public static String getTodayHour(Object date) { return getYearMonthDayHour(DateHelper.getDateTime(date)); } /** * 取得当前日期的字符串表示,格式为 yyyy-MM-dd HH:00:00 * * @param date * @return */ public static String getTodayHour(Date date) { return getYearMonthDayHour(date); } /** * 获取年份 * * @param date 需要处理的日期 * @return 获取到的年份 */ public static int getYear(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return c.get(Calendar.YEAR); } /** * 获取月份 * * @param date 需要处理的日期 * @return 获取到的月份 */ public static int getMonth(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return c.get(Calendar.MONTH); } /** * 获取日期 * * @param date 需要处理的日期 * @return 获取的天数 */ public static int getDay(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return c.get(Calendar.DAY_OF_MONTH); } /** * 获取当天的日期 * * @param date 需要的日期 * @return 返回的日期 */ public static Calendar getDayCalendarBegin(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); //当前起始日期 Calendar begin = Calendar.getInstance(); begin.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 0, 0, 0); begin.set(Calendar.MILLISECOND, 0); return begin; } /** * 获取当天的日期 * * @param date 需要的日期 * @param initMillSecond 初始化毫秒单位 * @return 返回的日期 */ public static Date initMillSecond(Date date, int initMillSecond) { if (initMillSecond == INIT_MILL_SECOND_NONE) { return date; } Calendar c = Calendar.getInstance(); c.setTime(date); if (initMillSecond == INIT_MILL_SECOND_START) { c.set(Calendar.MILLISECOND, 0); } else { c.set(Calendar.MILLISECOND, 999); } return c.getTime(); } private static void setDayEnd(Calendar cal) { cal.set(Calendar.HOUR_OF_DAY, 23); cal.set(Calendar.MINUTE, 59); cal.set(Calendar.SECOND, 59); cal.set(Calendar.MILLISECOND, 999); } private static void setDayStart(Calendar cal) { cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); } /** * 获取某一天起始和结束时间 * * @param date 某一天 * @return 返回的日期 */ public static Date getDayStart(Date date) { Calendar begin = getDayCalendarBegin(date); return begin.getTime(); } /** * 获取某一天起始和结束时间 * * @param date 某一天 * @return 返回的日期 */ public static Date getDayEnd(Date date) { Calendar end = getDayCalendarBegin(date); setDayEnd(end); return end.getTime(); } /** * 获取本周开始日期 * * @param date 某一天 * @return 返回的日期 */ public static Date getWeekStart(Date date) { Calendar cal = getDayCalendarBegin(date); // 周日 int weekOfYear; if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) { weekOfYear = cal.get(Calendar.WEEK_OF_YEAR) - 1; } else { weekOfYear = cal.get(Calendar.WEEK_OF_YEAR); } cal.setWeekDate(cal.getWeekYear(), weekOfYear, 2); setDayStart(cal); return cal.getTime(); } /** * 获取本周结束日期 * * @param date 某一天 * @return 返回的值 */ public static Date getWeekEnd(Date date) { Calendar cal = getDayCalendarBegin(date); // 周日 int weekOfYear; if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) { weekOfYear = cal.get(Calendar.WEEK_OF_YEAR); } else { weekOfYear = cal.get(Calendar.WEEK_OF_YEAR) + 1; } cal.setWeekDate(cal.getWeekYear(), weekOfYear, 1); setDayEnd(cal); return cal.getTime(); } /** * 获取本月开始时间 * * @param date 某一天 * @return 返回的日期 */ public static Date getMonthStart(Date date) { Calendar cal = getDayCalendarBegin(date); cal.set(Calendar.DAY_OF_MONTH, 1); setDayStart(cal); return cal.getTime(); } /** * 获得本月最后一天24点时间 * * @param date 某一天 * @return 返回的值 */ public static Date getMonthEnd(Date date) { Calendar cal = getDayCalendarBegin(date); cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); setDayEnd(cal); return cal.getTime(); } /** * 获取本季度开始时间 * * @param date 某一天 * @return 返回的日期 */ public static Date getMonth3Start(Date date) { Calendar cal = getDayCalendarBegin(date); int month = cal.get(Calendar.MONTH) / 3 * 3; cal.set(Calendar.MONTH, month); cal.set(Calendar.DAY_OF_MONTH, 1); setDayStart(cal); return cal.getTime(); } /** * 获得本月最后一天24点时间 * * @param date 某一天 * @return 返回的值 */ public static Date getMonth3End(Date date) { Calendar cal = getDayCalendarBegin(date); int month = cal.get(Calendar.MONTH) / 3 * 3; cal.set(Calendar.MONTH, month + 2); cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); setDayEnd(cal); return cal.getTime(); } /** * 获取本年开始时间 * * @param date 某一天 * @return 返回值 */ public static Date getYearStart(Date date) { Calendar cal = getDayCalendarBegin(date); cal.set(Calendar.MONTH, 0); cal.set(Calendar.DAY_OF_MONTH, 1); setDayStart(cal); return cal.getTime(); } /** * 获取本年结束时间 * * @param date 某一天 * @return 返回值 */ public static Date getYearEnd(Date date) { Calendar cal = getDayCalendarBegin(date); cal.set(Calendar.MONTH, 11); cal.set(Calendar.DAY_OF_MONTH, 31); setDayEnd(cal); return cal.getTime(); } /** * 当日期长度只有到日期时,在最后免补足时间 * * @param from * @param patchHour * @return */ public static String initDate(String from, String patchHour) { if (StringHelper.isEmpty(from)) { return from; } if (from.length() <= 10) { return from + patchHour; } else { return from; } } /** * 本季度开始时间 * * @param date 某一天 * @return 返回值 */ public static Date getQuarterStart(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); int currentMonth = c.get(Calendar.MONTH) + 1; SimpleDateFormat longSdf = new SimpleDateFormat(FORMAT_SECOND_STRING); SimpleDateFormat shortSdf = new SimpleDateFormat(FORMAT_DAY_STRING); Date now = null; try { if (currentMonth >= MONTH_1 && currentMonth <= MONTH_3) { c.set(Calendar.MONTH, 0); } else if (currentMonth >= MONTH_4 && currentMonth <= MONTH_6) { c.set(Calendar.MONTH, MONTH_3); } else if (currentMonth >= MONTH_7 && currentMonth <= MONTH_9) { c.set(Calendar.MONTH, MONTH_6); } else if (currentMonth >= MONTH_10 && currentMonth <= MONTH_12) { c.set(Calendar.MONTH, MONTH_9); } c.set(Calendar.DATE, 1); now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00"); } catch (Exception e) { e.printStackTrace(); } return now; } /** * 本季度的结束时间,即2012-03-31 23:59:59 * * @param date 某一天 * @return 返回值 */ public static Date getQuarterEnd(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(getQuarterStart(date)); cal.add(Calendar.MONTH, 3); cal.add(Calendar.MILLISECOND, -1); return cal.getTime(); } /** * 获取当前时间的年、月 * * @param format 需要的格式 * @return 返回值 */ public static String geYearMonth(String format) { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat(format); return sdf.format(date); } /** * 获取当前时间的年 * * @return 当前年份 */ public static String getCurrentYear() { Calendar calendar = Calendar.getInstance(); return calendar.get(Calendar.YEAR) + ""; } /** * 将时间格式转为yyyy-MM-dd HH:mm:ss格式 * * @param date 某一天 * @return 返回值 */ public static String parseToStr(Date date) { SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_SECOND_STRING); return sdf.format(date); } /** * 判断字符串日期是否为同一天 * * @param strFrom 对比的某一天 * @param strTo 对比的某一天 * @return 需要的返回值 */ public static boolean isEqualsDay(String strFrom, String strTo) { try { Date dateFrom = DateAutoHelper.getAutoDate(strFrom); Date dateTo = DateAutoHelper.getAutoDate(strTo); SimpleDateFormat format = new SimpleDateFormat(FORMAT_DAY_STRING); return format.format(dateFrom).equals(format.format(dateTo)); } catch (Exception e) { e.printStackTrace(); } return false; } /** * 判断两个日期是否为同一年(排除同月同天的情况) * * @param dateFrom 对比的某一天 * @param dateTo 对比的某一天 * @return 需要的返回值 */ public static boolean isEqualsYear(Date dateFrom, Date dateTo) { SimpleDateFormat format = new SimpleDateFormat("yyyy"); return format.format(dateFrom).equals(format.format(dateTo)); } /** * 判断两个日期是否为同一月 * * @param dateFrom 对比的某一天 * @param dateTo 对比的某一天 * @return 需要的返回值 */ public static boolean isEqualsMonth(Date dateFrom, Date dateTo) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM"); return format.format(dateFrom).equals(format.format(dateTo)); } /** * 计算两个日期之间相差的天数 * * @param from 较小的时间 * @param to 较大的时间 * @return 相差天数 */ public static long betweenDay(Date from, Date to) { long mill = betweenMillSecond(from, to, FORMAT_DAY_STRING); return mill / (1000 * 3600 * 24); } /** * 计算两个日期之间相差的秒数 * * @param from 较小的时间 * @param to 较大的时间 * @return 相差秒数 */ public static long betweenSecond(Date from, Date to) { long mill = betweenMillSecond(from, to, FORMAT_SECOND_STRING); return mill / 1000; } /** * 计算两个时间相差的毫秒数 * * @param from 开始时间 * @param to 结束时间 * @param format 通过format来确定精度 * @return 相差毫秒数 */ public static long betweenMillSecond(Date from, Date to, String format) { try { SimpleDateFormat sdf = new SimpleDateFormat(format); from = sdf.parse(sdf.format(from)); to = sdf.parse(sdf.format(to)); Calendar cal = Calendar.getInstance(); cal.setTime(from); long fromMill = cal.getTimeInMillis(); cal.setTime(to); long toMill = cal.getTimeInMillis(); long mill = toMill - fromMill; return mill; } catch (ParseException ex) { throw new RuntimeException(ex); } } /** * 获取周,从星期1开始,索引号为0 * * @param dt 传入时间 * @return 星期序号 */ public static int getWeek(Date dt) { Calendar cal = Calendar.getInstance(); cal.setTime(dt); int w = cal.get(Calendar.DAY_OF_WEEK); if (w < 1) { w = 1; } w = (w + 5) % 7; return w; } /** * 获取当前日期是星期几<br> * * @param dt 传入的时间 * @return 当前日期是星期几 */ public static String getWeekName(Date dt) { String[] weekDays = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"}; int w = getWeek(dt); return weekDays[w]; } /** * 根据时间字符串获取对应的周几 * * @param date 时间字符串 * @return 获取后的结果 */ public static String getWeekName1(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); int weekIndex = cal.get(Calendar.DAY_OF_WEEK) - 1; if (weekIndex < 0) { weekIndex = 0; } String[] weeks = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; return weeks[weekIndex]; } /** * 增加年份处理 * * @param date 当前日期 * @param year 需要增加的年份 * @return 增加之后的值 */ public static Date addYear(Date date, int year) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.YEAR, year); return cal.getTime(); } /** * 增加月份处理 * * @param date 当前日期 * @param month 需要增加的月份 * @return 增加之后的值 */ public static Date addMonth(Date date, int month) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.MONTH, month); return cal.getTime(); } /** * 增加日期处理 * * @param date 当前日期 * @param day 需要增加的日期 * @return 增加之后的值 */ public static Date addDay(Date date, int day) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.DATE, day); return cal.getTime(); } /** * 增加小时处理 * * @param date 当前日期 * @param hour 需要增加的小时 * @return 增加之后的值 */ public static Date addHour(Date date, int hour) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.HOUR, hour); return cal.getTime(); } /** * 增加分钟处理 * * @param date 当前日期 * @param minute 需要增加的分钟 * @return 增加之后的值 */ public static Date addMinute(Date date, int minute) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.MINUTE, minute); return cal.getTime(); } /** * 增加秒处理 * * @param date 当前日期 * @param second 需要增加的分钟 * @return 增加之后的值 */ public static Date addSecond(Date date, int second) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.SECOND, second); return cal.getTime(); } /** * 获取当前时间,精准到秒,格式为: yyyy-MM-dd HH:mm:ss * * @return 当前时间 */ public static String getNow() { return getDateTimeString(new Date()); } /** * 获取时间的字符串,精准到秒,为null则返回空字符串,格式为: yyyy-MM-dd HH:mm:ss * * @param date 需要获取的时间 * @return 获取到的结果 */ public static String getDateTimeString(Date date) { return getDateTimeString(FORMAT_SECOND_STRING, date); } /** * 获取时间为指定格式的字符串,为null则返回空字符串 * * @param format 格式 yyyy-MM-dd HH:mm:ss * @param date 字符串 * @return 转换后的结果 */ public static String getDateTimeString(String format, Date date) { String to = ""; if (date != null) { to = new SimpleDateFormat(format).format(date); } return to; } /** * 获取时间为指定格式的字符串,为null则返回空字符串 * * @param format 格式 yyyy-MM-dd HH:mm:ss * @param date 字符串 * @return 转换后的结果 */ public static String getDateTimeString(String format, String date) { Date dt = getDateTime(date); return getDateTimeString(format, dt); } /** * 比较时间 * * @param from 对比时间 * @param to 结束时间 * @return 对别结果 */ public static int compare(Date from, Date to) { if (from == null && to == null) { return 0; } else if (from == null) { return 1; } else if (to == null) { return -1; } else if (from.getTime() < to.getTime()) { return 1; } else if (from.getTime() > to.getTime()) { return -1; } else { return 0; } } /** * 将时间字符串格式化为 yyyy-MM-dd hh:mm:ss * * @param from 需要格式化的字符串 * @return 格式化后的结果 * @throws ParseException 可能抛出不能解释字符串的异常 */ public static String formatDateString(String from) { try { SimpleDateFormat format = new SimpleDateFormat(FORMAT_SECOND_STRING); Date dt = format.parse(from); return format.format(dt); } catch (ParseException e) { throw new RuntimeException(e); } } /** * 判断时间是否在时间段内 * * @param date 当前时间 yyyy-MM-dd HH:mm:ss * @param from 开始时间 00:00:00 * @param to 结束时间 00:05:00 * @return 是否在时间范围之内 */ public static boolean isBetween(Date date, String from, String to) { if (date == null) { return false; } Date fromDate = getDateTime(getDateTimeString(FORMAT_DAY_STRING + " " + from, date)); Date toDate = getDateTime(getDateTimeString(FORMAT_DAY_STRING + " " + to, date)); if (fromDate != null && date.getTime() < fromDate.getTime()) { return false; } return toDate == null || date.getTime() <= toDate.getTime(); } /** * 获取当前日期 * * @param from 当前时间 yyyy-MM-dd HH:mm:ss * @return 返回日期: yyyy-MM-dd */ public static String toDay(String from) { if (StringHelper.isEmpty(from)) { return from; } try { SimpleDateFormat format = new SimpleDateFormat(FORMAT_DAY_STRING); return format.format(format.parse(from)); } catch (ParseException e) { ExceptionHelper.handleException(DateHelper.class, e, from); } return StringHelper.EMPTY; } /** * 获取当前日期字符串 * * @param date 需要获取的日期 * @return 获取到的日期字符串 */ public static String toDay(Date date) { SimpleDateFormat format = new SimpleDateFormat(FORMAT_DAY_STRING); return format.format(date); } /** * 获取当前日期 * * @param date 需要获取的日期 * @return 获取到的日期字符串 */ public static String toDay(long date) { SimpleDateFormat format = new SimpleDateFormat(FORMAT_DAY_STRING); return format.format(new Date(date)); } /** * 获取指定时间月份的第一天 * * @param time 需要获取的时间 * @return 指定时间月份的第一天 */ public static String toMonth(Date time) { return getDateTimeString(FORMAT_MONTH_STRING, time); } /** * 获取指定时间年份的第一天 * * @param time 需要获取的时间 * @return 指定时间年份的第一天 */ public static String toYear(Date time) { return getDateTimeString(FORMAT_YEAR_STRING, time); } /** * 判断是否时时间格式字符串 * * @param from 来源字符串 * @return 是否时时间字符串 */ public static boolean isDateFormat(String from) { try { return DateAutoHelper.getAutoDate(from) != null; } catch (Exception ex) { return false; } } private static final long MINUTE_UNIT = 1000 * 60; private static final long HOUR_MINUTE_UNIT = 15; /** * 获取小时中的刻度时间 * * @param from 来源时间 * @return */ public static String getHourUnitTime(Object from) { return getHourUnitTime(from, HOUR_MINUTE_UNIT); } /** * 获取小时中的刻度时间 * * @param from 来源时间 * @return */ public static String getHourUnitTime(Object from, long minute) { Date dateTime = getDateTime(from); if (dateTime == null) { return null; } // 获取当前小时,用于累加 Date hour = DateHelper.getDateTime(DateHelper.getDateTimeString("yyyy-MM-dd HH:00:00.000", dateTime)); // 获取当前小时毫秒数量 long totalMill = dateTime.getTime() - hour.getTime(); // 计算分钟单位 long unit = totalMill / MINUTE_UNIT / minute; // 计算四舍五入后的时间 Date to = new Date(hour.getTime() + unit * MINUTE_UNIT * minute); // 将时间转换为字符串 return DateHelper.getDateTimeString(to); } /** * 获取小时中的刻度时间 * * @param from 来源时间 * @return */ public static int getMonthBetween(Object from, Object to) { Date fromDate = getDateTime(from); Date toDate = getDateTime(to); if (from == null || toDate == null) { return 0; } Calendar bef = Calendar.getInstance(); bef.setTime(fromDate); Calendar aft = Calendar.getInstance(); aft.setTime(getDateTime(toDate)); int surplus = aft.get(Calendar.DATE) - bef.get(Calendar.DATE); int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH); int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12; surplus = surplus <= 0 ? 1 : 0; return (Math.abs(month + result) + surplus); } }