时间工具类
此工具类主要实现了获取指定日期开始时间戳/结束时间戳,指定格式的时间格式化,如将时间转换成整十分钟、整三十分钟、整十二小时,具体的用法代码可见main方法。
实现方案结合的日历类,可能有更优的方案,大家可以一起探讨下。
public class DateUtil { public static final DateTimeFormatter DTF = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); public static final SimpleDateFormat NORM_SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); /**一年中的第几周**/ public static final SimpleDateFormat WEEK_SDF = new SimpleDateFormat("yyyy-ww"); /** * 获取今天开始时间和结束时间 * * @return */ public static Map getTodayTime() { Long startTime = getStartTime(); //当期时间 Long endTime = getEndTime(); String startTimeStr = DTF.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(startTime), ZoneId.systemDefault())); String endTimeStr = DTF.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(endTime), ZoneId.systemDefault())); Map map = new HashMap(); map.put("startDate", startTimeStr); map.put("endDate", endTimeStr); return map; } /** * 获取今天日期 年月日 **/ public static Date getTodayYmd() { Date now = new Date(); Calendar cal1 = Calendar.getInstance(); cal1.setTime(now); // 将时分秒,毫秒域清零 cal1.set(Calendar.HOUR_OF_DAY, 0); cal1.set(Calendar.MINUTE, 0); cal1.set(Calendar.SECOND, 0); cal1.set(Calendar.MILLISECOND, 0); return cal1.getTime(); } /** * 获取昨天日期 年月日 **/ public static Date getYesterdayYmd() { Calendar cal = new GregorianCalendar(); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); cal.add(Calendar.DAY_OF_MONTH, -1); return cal.getTime(); } /** * 获取昨天开始时间和结束时间 * * @return */ public static Map getYesterdayTime() { Long startTime = getBeginDayOfYesterday(); Long endTime = getEndDayOfYesterDay(); String startTimeStr = DTF.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(startTime), ZoneId.systemDefault())); String endTimeStr = DTF.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(endTime), ZoneId.systemDefault())); Map map = new HashMap(); map.put("startDate", startTimeStr); map.put("endDate", endTimeStr); return map; } /** * 获取当月开始时间和结束时间 * * @return */ public static Map getMonthTime() { Long startTime = getMonthStartTime(); Long endTime = getMonthEndTime(); String startTimeStr = DTF.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(startTime), ZoneId.systemDefault())); String endTimeStr = DTF.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(endTime), ZoneId.systemDefault())); Map map = new HashMap(); map.put("startDate", startTimeStr); map.put("endDate", endTimeStr); return map; } /** * 获取上个月开始时间和结束时间 * * @return */ public static Map getLastMonthTime() { Long startTime = getLastMonthStartTime(); Long endTime = getLastMonthEndTime(); String startTimeStr = DTF.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(startTime), ZoneId.systemDefault())); String endTimeStr = DTF.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(endTime), ZoneId.systemDefault())); Map map = new HashMap(); map.put("startDate", startTimeStr); map.put("endDate", endTimeStr); return map; } /** * 获取本年的开始时间和结束时间 **/ public static Map getThisYearTime() { Long startTime = getBeginDayOfYear(); Long endTime = getEndDayOfYear(); DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd"); String startTimeStr = ftf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(startTime), ZoneId.systemDefault())); String endTimeStr = ftf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(endTime), ZoneId.systemDefault())); Map map = new HashMap(); map.put("startDate", startTimeStr); map.put("endDate", endTimeStr); return map; } /** * 获取今天开始时间 */ public static Long getStartTime() { Calendar todayStart = Calendar.getInstance(); todayStart.set(Calendar.HOUR_OF_DAY, 0); todayStart.set(Calendar.MINUTE, 0); todayStart.set(Calendar.SECOND, 0); todayStart.set(Calendar.MILLISECOND, 0); return todayStart.getTimeInMillis(); } /** * 获取今天结束时间 */ public static Long getEndTime() { Calendar todayEnd = Calendar.getInstance(); todayEnd.set(Calendar.HOUR_OF_DAY, 23); todayEnd.set(Calendar.MINUTE, 59); todayEnd.set(Calendar.SECOND, 59); todayEnd.set(Calendar.MILLISECOND, 999); return todayEnd.getTimeInMillis(); } /** * 获取当月开始时间 */ public static Long getMonthStartTime() { Long currentTime = System.currentTimeMillis(); String timeZone = "GMT+8:00"; // 获取当前日期 Calendar calendar = Calendar.getInstance(); calendar.setTimeZone(TimeZone.getTimeZone(timeZone)); calendar.setTimeInMillis(currentTime); calendar.add(Calendar.YEAR, 0); calendar.add(Calendar.MONTH, 0); // 设置为1号,当前日期既为本月第一天 calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); return calendar.getTimeInMillis(); } /** * 获取当月的结束时间戳 */ public static Long getMonthEndTime() { Long currentTime = System.currentTimeMillis(); String timeZone = "GMT+8:00"; // 获取当前日期 Calendar calendar = Calendar.getInstance(); calendar.setTimeZone(TimeZone.getTimeZone(timeZone)); calendar.setTimeInMillis(currentTime); calendar.add(Calendar.YEAR, 0); calendar.add(Calendar.MONTH, 0); // 获取当前月最后一天 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.getTimeInMillis(); } /** * 获取上个月开始时间戳 **/ public static Long getLastMonthStartTime() { Long currentTime = System.currentTimeMillis(); String timeZone = "GMT+8:00"; // 获取当前日期 Calendar calendar = Calendar.getInstance(); calendar.setTimeZone(TimeZone.getTimeZone(timeZone)); calendar.setTimeInMillis(currentTime); calendar.add(Calendar.YEAR, 0); calendar.add(Calendar.MONTH, -1); // 设置为1号,当前日期既为本月第一天 calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); return calendar.getTimeInMillis(); } /** * 获取上个月结束时间戳 **/ public static Long getLastMonthEndTime() { Long currentTime = System.currentTimeMillis(); String timeZone = "GMT+8:00"; // 获取当前日期 Calendar calendar = Calendar.getInstance(); calendar.setTimeZone(TimeZone.getTimeZone(timeZone)); calendar.setTimeInMillis(currentTime); calendar.add(Calendar.YEAR, 0); calendar.add(Calendar.MONTH, -1); // 获取当前月最后一天 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.getTimeInMillis(); } /** * 获取昨天开始时间戳 **/ public static Long getBeginDayOfYesterday() { Calendar cal = new GregorianCalendar(); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); cal.add(Calendar.DAY_OF_MONTH, -1); return cal.getTimeInMillis(); } /** * 获取昨天结束时间戳 **/ public static Long getEndDayOfYesterDay() { Calendar cal = new GregorianCalendar(); cal.set(Calendar.HOUR_OF_DAY, 23); cal.set(Calendar.MINUTE, 59); cal.set(Calendar.SECOND, 59); cal.add(Calendar.DAY_OF_MONTH, -1); return cal.getTimeInMillis(); } /** * 获取今年是哪一年 **/ public static Integer getNowYear() { Date date = new Date(); GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance(); gc.setTime(date); return gc.get(1); } /** * 获取本年的开始时间戳 **/ public static Long getBeginDayOfYear() { Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, getNowYear()); cal.set(Calendar.MONTH, Calendar.JANUARY); cal.set(Calendar.DATE, 1); return getDayStartTime(cal.getTime()); } /** * 获取本年的结束时间戳 **/ public static Long getEndDayOfYear() { Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, getNowYear()); cal.set(Calendar.MONTH, Calendar.DECEMBER); cal.set(Calendar.DATE, 31); return getDayEndTime(cal.getTime()); } /***获取某个日期的开始时间戳**/ public static Long getDayStartTime(Date d) { ObjectUtil.notNull(d, "未指定日期"); Calendar calendar = Calendar.getInstance(); calendar.setTime(d); calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0); calendar.set(Calendar.MILLISECOND, 0); return calendar.getTimeInMillis(); } /** * 获取某个日期的结束时间 **/ public static Long getDayEndTime(Date d) { ObjectUtil.notNull(d, "未指定日期"); Calendar calendar = Calendar.getInstance(); calendar.setTime(d); calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59); calendar.set(Calendar.MILLISECOND, 999); return calendar.getTimeInMillis(); } /** * round为-1,向下取一个period 比如 2022-02-16 11:11:20 period =10,则输出2022-02-16 00:00:00 * round为0,取本次period 比如 2022-02-16 11:11:20 period =10,则输出2022-02-16 10:00:00 * round为1,向上取一个period 比如 2022-02-16 11:11:20 period =10,则输出2022-02-16 20:00:00 * @param timeStr 时间 * @param round 轮次标示 * @param period 周期 * @return java.lang.String * @author Jackpot * @date 2022/2/16 11:10 AM */ public static String minuteFormat(String timeStr, Integer round, Integer period) { round = round * period; Calendar calendar = Calendar.getInstance(); try { calendar.setTime(NORM_SDF.parse(timeStr)); } catch (ParseException e) { e.printStackTrace(); } int minute = calendar.get(Calendar.MINUTE); minute = Math.round(minute / period * period); calendar.set(Calendar.MINUTE, minute + round); calendar.set(Calendar.SECOND, 0); String time = NORM_SDF.format(calendar.getTime()); return time; } /** * round为-1,向下取一个period 比如 2022-02-16 11:11:20 period =12,则输出2022-02-15 12:00:00 * round为0,取本次period 比如 2022-02-16 11:11:20 period =12,则输出2022-02-16 00:00:00 * round为1,向上取一个period 比如 2022-02-16 11:11:20 period =12,则输出2022-02-16 12:00:00 * @param timeStr 时间 * @param round 轮次标示 * @param period 周期 * @return java.lang.String * @author Jackpot * @date 2022/2/16 12:00 PM */ public static String hourFormat(String timeStr, Integer round, Integer period) { round = round * period; Calendar calendar = Calendar.getInstance(); try { calendar.setTime(NORM_SDF.parse(timeStr)); } catch (ParseException e) { e.printStackTrace(); } int hour = calendar.get(Calendar.HOUR); calendar.set(Calendar.HOUR, Math.round(hour / period * period) + round); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); String time = NORM_SDF.format(calendar.getTime()); return time; } /** * round为-1,向下取一个period 比如 2022-02-16 11:11:20 period =1,则输出2022-02-15 00:00:00 * round为0,取本次period 比如 2022-02-16 11:11:20 period =1,则输出2022-02-16 00:00:00 * round为1,向上取一个period 比如 2022-02-16 11:11:20 period =1,则输出2022-02-17 00:00:00 * @param timeStr 时间 * @param round 轮次标示 * @param period 周期 * @return java.lang.String * @author Jackpot * @date 2022/2/16 12:00 PM */ public static String dayFormat(String timeStr, Integer round, Integer period) { round = round * period; Calendar calendar = Calendar.getInstance(); try { calendar.setTime(NORM_SDF.parse(timeStr)); } catch (ParseException e) { e.printStackTrace(); } int day = calendar.get(Calendar.DATE); calendar.set(Calendar.DATE, Math.round(day / period * period) + round); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); String time = NORM_SDF.format(calendar.getTime()); return time; } /** * round为-1,向下取一个period 比如 2022-02-16 11:11:20 period =1,则输出2022-07(yyyy-ww格式:当年第几周) * round为0,取本次period 比如 2022-02-16 11:11:20 period =1,则输出2022-08 * round为1,向上取一个period 比如 2022-02-16 11:11:20 period =1,则输出2022-09 * @param timeStr 时间 * @param round 轮次标示 * @param period 周期 * @return java.lang.String * @author Jackpot * @date 2022/2/16 11:10 AM */ public static String weekFormat(String timeStr, Integer round, Integer period) { round = round * period; Calendar calendar = Calendar.getInstance(); try { calendar.setTime(NORM_SDF.parse(timeStr)); } catch (ParseException e) { e.printStackTrace(); } int week = calendar.get(Calendar.WEEK_OF_YEAR); calendar.set(Calendar.WEEK_OF_YEAR, Math.round(week / period * period) + round); String time = WEEK_SDF.format(calendar.getTime()); return time; } /** * round为-1,向下取一个period 比如 2022-02-16 11:11:20 period =1,则输出2022-01-01 00:00:00 * round为0,取本次period 比如 2022-02-16 11:11:20 period =1,则输出2022-02-01 00:00:00 * round为1,向上取一个period 比如 2022-02-16 11:11:20 period =1,则输出2022-03-01 00:00:00 * @param timeStr 时间 * @param round 轮次标示 * @param period 周期 * @return java.lang.String * @author Jackpot * @date 2022/2/16 11:10 AM */ public static String monthFormat(String timeStr, Integer round, Integer period) { round = round * period; Calendar calendar = Calendar.getInstance(); try { calendar.setTime(NORM_SDF.parse(timeStr)); } catch (ParseException e) { e.printStackTrace(); } int month = calendar.get(Calendar.MONTH); calendar.set(Calendar.MONTH, Math.round(month / period * period) + round); calendar.set(Calendar.DATE, 1); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); String time = NORM_SDF.format(calendar.getTime()); return time; } public static void main(String[] args) { Map todayTime = getTodayTime(); System.out.println("今天:" + JSONObject.toJSONString(todayTime)); Map yesterdayTime = getYesterdayTime(); System.out.println("昨天:" + JSONObject.toJSONString(yesterdayTime)); Map monthTime = getMonthTime(); System.out.println("当月:" + JSONObject.toJSONString(monthTime)); Map lastMonthTime = getLastMonthTime(); System.out.println("上月:" + JSONObject.toJSONString(lastMonthTime)); Map yearTime = getThisYearTime(); System.out.println("当年:" + JSONObject.toJSONString(yearTime)); System.out.println(minuteFormat("2022-02-16 13:11:30", -1, 10)); System.out.println(minuteFormat("2022-02-16 11:11:30", -1, 30)); System.out.println(hourFormat("2022-02-16 11:20:30", 1, 12)); System.out.println(dayFormat("2022-02-16 19:00:30", -1, 1)); System.out.println(weekFormat("2022-02-16 19:00:30", -1, 2)); System.out.println(monthFormat("2022-02-16 19:00:30", -1, 1)); } }