package com.hx.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; public class DateUtil { private static SimpleDateFormat Format_1 = new SimpleDateFormat("yyyyMMdd"); private static SimpleDateFormat Format_2 = new SimpleDateFormat("yyyyMMddHHmmss"); private static SimpleDateFormat Format_3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private static SimpleDateFormat Format_4 = new SimpleDateFormat("yyyy-MM-dd"); private static SimpleDateFormat Format_5 = new SimpleDateFormat("HH:mm:ss"); private static SimpleDateFormat Format_6 = new SimpleDateFormat("yyyyMM"); private static SimpleDateFormat Format_7 = new SimpleDateFormat("MM月dd日 HH:mm"); private static SimpleDateFormat Format_8 = new SimpleDateFormat("yyyy年MM月"); private static SimpleDateFormat Format_9 = new SimpleDateFormat("MM-dd"); private static SimpleDateFormat Format_10 = new SimpleDateFormat("yyyy年MM月dd日"); private static SimpleDateFormat Format_11 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); private static SimpleDateFormat Format_12 = new SimpleDateFormat("yyyyMMddHHmm"); private static SimpleDateFormat Format_13 = new SimpleDateFormat("yyyy/MM/dd"); private static SimpleDateFormat Format_14 = new SimpleDateFormat("yyyy-MM"); private static SimpleDateFormat Format_15 = new SimpleDateFormat("yyyyMMddHHmmssSSS"); private static SimpleDateFormat Format_16 = new SimpleDateFormat("yyyy/MM/dd HH:mm"); private static SimpleDateFormat Format_17 = new SimpleDateFormat("HH:mm"); /**时间格式转化iso8601 * @param date 时间 * @return 返回的时间格式字符串 */ public static String dateFormatISO8601(Date date) { if(!SimpleTool.checkNotNull(date)){ return ""; } SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");//设置日期格式 return df.format(date); } /**时间格式转化 * @param date 时间 * @param format 时间格式 * @return 返回的时间格式字符串 */ public static String dateFormat(Date date, String format) { if(!SimpleTool.checkNotNull(date)){ return ""; } SimpleDateFormat df = new SimpleDateFormat(format);//设置日期格式 return df.format(date); } /**时间戳转时间 * @param timestamp 时间戳 * @param format 时间格式 * @return 返回的时间格式字符串 */ public static Date timestampToDate(long timestamp, String format) { SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String sd = sdf.format(new Date(timestamp)); // 时间戳转换成时间 return DateUtil.parseString(sd,"yyyy-MM-dd HH:mm:ss"); } /** * 转换成yyyyMMdd格式的日期字符串 * * @param date * @return */ public static String formatDate(Date date) { return Format_1.format(date); } /** * 转换成yyyyMMddHHmmss格式的日期字符串 * * @param date * @return */ public static String formatDate_1(Date date) { return Format_2.format(date); } /** * 转换成yyyy-MM-dd HH:mm:ss格式的日期字符串 * * @param date * @return */ public static String formatDate_2(Date date) { return date == null ? null : Format_3.format(date); } /** * 转换成yyyy-MM-dd格式的日期字符串 * * @param date * @return */ public static String formatDate_3(Date date) { return date != null ? Format_4.format(date) : null; } /** * 转换成HH:mm:ss格式的日期字符串 * * @param date * @return */ public static String formatDate_4(Date date) { return Format_5.format(date); } /** * 转换成yyyyMM格式的日期字符串 * * @param date * @return */ public static String formatDate_5(Date date) { return Format_6.format(date); } /** * 转换成mm月dd日 HH:mm格式的日期字符串 * * @param date * @return */ public static String formatDate_6(Date date) { return Format_7.format(date); } /** * 转换成yyyy年mm月格式的日期字符串 * * @param date * @return */ public static String formatDate_7(Date date) { return Format_8.format(date); } /** * 转换成mm-dd格式的日期字符串 * * @param date * @return */ public static String formatDate_8(Date date) { return Format_9.format(date); } /** * 转换成yyyy年MM月dd日格式的日期字符串 * * @param date * @return */ public static String formatDate_9(Date date) { return Format_10.format(date); } /** * 转换成yyyyMMddHHmm格式的日期字符串 * * @param date * @return */ public static String formatDate_11(Date date) { return Format_12.format(date); } /** * 转换成yyyy年MM月dd日 HH:mm:ss格式的日期字符串 * * @param date * @return */ public static String formatDate_10(Date date) { return Format_11.format(date); } /** * 转换成yyyy/MM/dd 格式的日期字符串 * * @param date * @return */ public static String formatDate_13(Date date) { return Format_13.format(date); } /** * 转换成yyyy-MM 格式的日期字符串 * * @param date * @return */ public static String formatDate_14(Date date) { return Format_14.format(date); } /** * 当前时间之前的时间与当前时间相差多少秒 * @param startDate 当前时间之前的时间 * @return */ public static int calLastedTime(Date startDate) { long nowDate = new Date().getTime(); long startDateTime = startDate.getTime(); int diffSeconds = (int) ((nowDate - startDateTime) / 1000); return diffSeconds; } /** * 转换成yyyyMMddHHmmssSSS格式的日期字符串 * * @param date * @return */ public static String formatDate_15(Date date) { return Format_15.format(date); } /** * 转换成yyyy/MM/dd HH:mm格式的日期字符串 * * @param date * @return */ public static String formatDate_16(Date date) { return Format_16.format(date); } /** * 转换成HH:mm格式的日期字符串 * * @param date * @return */ public static String formatDate_17(Date date) { return Format_17.format(date); } /** * 转换成字符串yyyyMMddHHmmss成日�? * * @param str * @return * @throws Exception */ public static Date parseString(String str) { try { return Format_2.parse(str); }catch (Exception e) { return null; } } /** * 转换成字符串到指定的日期 * * @param str * @param format * @return * @throws Exception */ public static Date parseString(String str, String format) { SimpleDateFormat sdf = new SimpleDateFormat(format); try{ return sdf.parse(str); }catch (Exception e) { return null; } } /**字符串转成时间yyyy-MM-dd HH:mm:ss*/ public static Date parseString_1(String str) { try{ return Format_3.parse(str); }catch (Exception e) { return null; } } /**字符串转成时间yyyy-MM-dd*/ public static Date parseString_2(String str) { try{ return Format_4.parse(str); }catch (Exception e) { return null; } } /**时间上秒叠加 * @Author: ChenJiaHe * @param dateTime 时间 * @param second 秒 * @return */ public static Date addSecond(Date dateTime,int second){ Calendar c = Calendar.getInstance(); c.setTime(dateTime); c.add(Calendar.SECOND, second); return c.getTime(); } /**时间上分钟叠加 * @Author: ChenJiaHe * @param dateTime 时间 * @param min 分钟 * @return */ public static Date addMin(Date dateTime,int min){ Calendar c = Calendar.getInstance(); c.setTime(dateTime); c.add(Calendar.MINUTE, min); return c.getTime(); } /**时间上小时叠加 * @Author: ChenJiaHe * @param dateTime 时间 * @param hour 小时 * @return */ public static Date addhour(Date dateTime,int hour){ Calendar c = Calendar.getInstance(); c.setTime(dateTime); c.add(Calendar.HOUR, hour); return c.getTime(); } /**时间上天数叠加 * @Author: ChenJiaHe * @param dateTime 时间 * @param dayNum 天数 * @return */ public static Date addDay(Date dateTime,int dayNum){ Calendar c = Calendar.getInstance(); c.setTime(dateTime); c.add(Calendar.DATE, dayNum); return c.getTime(); } /**时间上月数叠加 * @Author: ChenJiaHe * @param dateTime 时间 * @param dayNum 天数 * @return */ public static Date addMonth(Date dateTime,int dayNum){ Calendar c = Calendar.getInstance(); c.setTime(dateTime); c.add(Calendar.MONTH, dayNum); return c.getTime(); } /**时间上年数叠加 * @Author: ChenJiaHe * @param dateTime 时间 * @param dayNum 天数 * @return */ public static Date addYear(Date dateTime,int dayNum){ Calendar c = Calendar.getInstance(); c.setTime(dateTime); c.add(Calendar.YEAR, dayNum); return c.getTime(); } /** * 转换成字符串到指定的日期 * * @param date * @param format * @return * @throws Exception */ public static String formatDate(Date date, String format) { if (date == null) { return null; } SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA); return sdf.format(date); } /**日期转星期 * @param dateTime 字符串时间 * @param format 字符串时间格式 * @param returnType 返回类型 0星期几1周几 * @return 数据 */ public static String dateToWeek(String dateTime,String format,Integer returnType) throws ParseException { SimpleDateFormat f = new SimpleDateFormat(format); String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; if(SimpleTool.checkNotNull(returnType)&&returnType==1){ weekDays = new String[]{"周日", "周一", "周二", "周三", "周四", "周五", "周六"}; } Calendar cal = Calendar.getInstance(); // 获得一个日历 Date datet = null; datet = f.parse(dateTime); cal.setTime(datet); int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。 if (w < 0) w = 0; return weekDays[w]; } /**日期转星期 * @param dateTime 时间 * @param returnType 返回类型 0星期几1周几 * @return 数据 */ public static String dateToWeek(Date dateTime,Integer returnType) { String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; if(SimpleTool.checkNotNull(returnType)&&returnType==1){ weekDays = new String[]{"周日", "周一", "周二", "周三", "周四", "周五", "周六"}; } Calendar cal = Calendar.getInstance(); // 获得一个日历 cal.setTime(dateTime); int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。 if (w < 0) w = 0; return weekDays[w]; } /** * 获取两个时间相差分钟数 * @param startTime 开始时间 * @param endTime 结束时间 * @return 分钟 */ public static long differMinute(Date startTime, Date endTime) { long NTime = startTime.getTime(); long OTime = endTime.getTime(); return (NTime - OTime) / 1000 / 60; } /** * endTime比startTime多的天数 * @param startTime 最小时间 * @param endTime 最大时间 * @return 返回 */ public static Integer differDay(Date startTime,Date endTime){ Calendar cal1 = Calendar.getInstance(); cal1.setTime(startTime); Calendar cal2 = Calendar.getInstance(); cal2.setTime(endTime); int day1= cal1.get(Calendar.DAY_OF_YEAR); int day2 = cal2.get(Calendar.DAY_OF_YEAR); int year1 = cal1.get(Calendar.YEAR); int year2 = cal2.get(Calendar.YEAR); if(year1 != year2) { //同一年 int timeDistance = 0 ; for(int i = year1 ; i < year2 ; i ++) { if(i%4==0 && i%100!=0 || i%400==0) {//闰年 timeDistance += 366; } else {//不是闰年 timeDistance += 365; } } return timeDistance + (day2-day1) ; } else { //不同年 System.out.println("判断day2 - day1 : " + (day2-day1)); return day2-day1; } } /**判断两个时间是不是同一天*/ public static boolean timeEqual(Date startTime,Date endTime){ if(startTime == null || endTime==null){ return false; } boolean status = false; if(formatDate(startTime,"yyyyMMdd").equals(formatDate(endTime,"yyyyMMdd"))){ status = true; } return status; } /**把秒转换成X天X时X分X秒*/ public static String getChineseStr(Integer second) { int day = 24 * 60 * 60; int hour = 60 * 60; int min = 60; int dayNum = second / day; int hourNum = second % day / hour; int minNum = second % day % hour / min; second = second % day % hour % min; String str = dayNum > 0 ? dayNum + "天" : ""; str += hourNum > 0 ? hourNum + "时" : ""; str += minNum > 0 ? minNum + "分" : ""; str += second + "秒"; return str; } /** * 针对str格式的时间做转换 格式为"xx:xx" * @param time 传入的时间 * @return 返回分钟如果10:25,则返回625 */ public static int getMinuteNum(String time){ if(!StringUtils.isEmpty(time)) { String[] arr = time.split(":"); if(arr != null && arr.length == 2) { return Integer.parseInt(arr[0]) * 60 + Integer.parseInt(arr[1]); } } return 0; } /** * 获取当前月的开始时间 * @param time 时间 * @return 返回时间 格式yyyy-MM-dd 00:00:00 */ public static Date getMonthStart(Date time) { Calendar calendar = Calendar.getInstance(); calendar.setTime(time); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); return calendar.getTime(); } /** * 获取当前月的开始时间 * * @param num 0拿取当月,正代表后,负代表前,值为几个(月) * @return 返回时间 格式yyyy-MM-dd 00:00:00 */ public static String getMonthStart(Integer num) { Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MONTH, num); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA); return sdf.format(calendar.getTime()); } /** * 获取当前月的结束时间 * @param date 点前时间 * @return 返回时间 格式yyyy-MM-dd 23:59:59 */ 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); return calendar.getTime(); } /** * 获取当前月的结束时间 * @param num 0拿取当月,正代表后,负代表前,值为几个(月) * @return 返回时间 格式yyyy-MM-dd 23:59:59 */ public static String getMonthEnd(Integer num) { Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MONTH, num); 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); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA); return sdf.format(calendar.getTime()); } /** * 获取当前年的开始时间 * @param time 时间 * @return 返回时间 格式yyyy-MM-dd 00:00:00 */ public static Date getYearStart(Date time) { Calendar calendar = Calendar.getInstance(); calendar.setTime(time); calendar.set(Calendar.MONTH, 0); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); return calendar.getTime(); } /** * 获取当前年的结束时间 * @param time 时间 * @return 返回时间 格式yyyy-MM-dd 00:00:00 */ 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); return calendar.getTime(); } /**这天的开始时间 * 日期2000-01-01变2000-01-01 00:00:00 */ public static String dayToStart(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); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA); return sdf.format(calendar.getTime()); } /**这天的最后时间 * 日期2000-01-01变2000-01-01 23:59:59 */ public static String dayToEnd(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); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA); 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 */ 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); return calendar.getTime(); } /**获取月份的天数 * @param date 时间 * @return 月份的天数 */ public static int getMonthDays(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.DATE, 1); cal.roll(Calendar.DATE, -1); return cal.getActualMaximum(Calendar.DATE); } /**获取月份的天数 * @param year 年份 * @param month 月份 * @return 月份的天数 */ public static int getMonthDays(int year, int month) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, (month - 1)); cal.set(Calendar.DATE, 1); cal.roll(Calendar.DATE, -1); return cal.getActualMaximum(Calendar.DATE); } /**获取月份的天数 * @param yearMonth 年月 * @param format 时间格式 * @return 返回 */ public static int getMonthDays(String yearMonth,String format) { SimpleDateFormat sdf = new SimpleDateFormat(format); Calendar calendar = Calendar.getInstance(); try { calendar.setTime(sdf.parse(yearMonth)); } catch (ParseException e) { e.printStackTrace(); } calendar.getActualMaximum(Calendar.DAY_OF_MONTH); return calendar.getActualMaximum(Calendar.DAY_OF_MONTH); } /**当天多少点前 * @param date 时间 * @param num 多少点前,不含当点前,24小时制 * @return 月份的天数 */ public static boolean getFrontMinute(Date date,int num) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.HOUR_OF_DAY, num-1); cal.set(Calendar.MINUTE, 59); cal.set(Calendar.SECOND, 59); return date.compareTo(cal.getTime()) < 1; } /** * 获得某个日期的当天某点 * 例如:2022-12-26 11:20:00 -> 2022-12-26 13:00:00 * @param num 24小时 */ public static String getHourDayTime(Date date,int num) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.HOUR_OF_DAY, num); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA); return sdf.format(calendar.getTime()); } /** * 获取时间当月剩余天数 * */ public static Integer getMonthSurplus(Date date) { Calendar month = Calendar.getInstance(); month.setTime(new Date()); month.set(Calendar.DATE, 1); 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(); } }