From 691dee7a71996cd99fdfe30bd61b2da95cc88ecd Mon Sep 17 00:00:00 2001 From: ANDRU-PC\Andru <769883346@qq.com> Date: 星期一, 18 九月 2023 16:04:51 +0800 Subject: [PATCH] 添加工具类方法 --- src/main/java/com/hx/util/DateUtil.java | 409 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 399 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/hx/util/DateUtil.java b/src/main/java/com/hx/util/DateUtil.java index 875014b..2943ccd 100644 --- a/src/main/java/com/hx/util/DateUtil.java +++ b/src/main/java/com/hx/util/DateUtil.java @@ -26,6 +26,18 @@ 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 鏃堕棿鏍煎紡 @@ -37,6 +49,17 @@ } 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"); } /** @@ -179,6 +202,20 @@ 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; + } + + /** * 杞崲鎴恲yyyMMddHHmmssSSS鏍煎紡鐨勬棩鏈熷瓧绗︿覆 * @@ -216,8 +253,13 @@ * @return * @throws Exception */ - public static Date parseString(String str) throws Exception { - return Format_2.parse(str); + public static Date parseString(String str) { + try { + return Format_2.parse(str); + }catch (Exception e) + { + return null; + } } /** @@ -228,9 +270,14 @@ * @return * @throws Exception */ - public static Date parseString(String str, String format) throws Exception { + public static Date parseString(String str, String format) { SimpleDateFormat sdf = new SimpleDateFormat(format); - return sdf.parse(str); + try{ + return sdf.parse(str); + }catch (Exception e) + { + return null; + } } /**瀛楃涓茶浆鎴愭椂闂磞yyy-MM-dd HH:mm:ss*/ @@ -255,6 +302,18 @@ } } + /**鏃堕棿涓婄鍙犲姞 + * @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 @@ -292,6 +351,19 @@ 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(); } @@ -368,10 +440,22 @@ } /** + * 鑾峰彇涓や釜鏃堕棿鐩稿樊鍒嗛挓鏁� + * @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姣攕tartTime澶氱殑澶╂暟 * @param startTime 鏈�灏忔椂闂� * @param endTime 鏈�澶ф椂闂� - * @return + * @return 杩斿洖 */ public static Integer differDay(Date startTime,Date endTime){ Calendar cal1 = Calendar.getInstance(); @@ -410,14 +494,13 @@ } boolean status = false; if(formatDate(startTime,"yyyyMMdd").equals(formatDate(endTime,"yyyyMMdd"))){ - status = false; + status = true; } return status; } /**鎶婄杞崲鎴怷澶鏃禭鍒哫绉�*/ - public static String getChineseStr(Integer second) - { + public static String getChineseStr(Integer second) { int day = 24 * 60 * 60; int hour = 60 * 60; int min = 60; @@ -452,8 +535,314 @@ } 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鎷垮彇褰撴湀锛屾浠h〃鍚庯紝璐熶唬琛ㄥ墠锛屽�间负鍑犱釜锛堟湀锛� + * @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鎷垮彇褰撴湀锛屾浠h〃鍚庯紝璐熶唬琛ㄥ墠锛屽�间负鍑犱釜锛堟湀锛� + * @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(); + } + + /** + * 鏍规嵁鍑虹敓骞存湀鏃ヨ绠楀勾榫� + * @param birth + * @return + */ + public static int getAge(Date birth) { + Calendar cal = Calendar.getInstance(); + int thisYear = cal.get(Calendar.YEAR); + int thisMonth = cal.get(Calendar.MONTH); + int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH); + + cal.setTime(birth); + int birthYear = cal.get(Calendar.YEAR); + int birthMonth = cal.get(Calendar.MONTH); + int birthdayOfMonth = cal.get(Calendar.DAY_OF_MONTH); + + int age = thisYear - birthYear; + + // 鏈冻鏈� + if (thisMonth <= birthMonth) { + // 褰撴湀 + if (thisMonth == birthMonth) { + // 鏈冻鏃� + if (dayOfMonth < birthdayOfMonth) { + age--; + } + } else { + age--; + } + } + return age; + } + + /** + * 鑾峰彇鏌愬ぉ缁撴潫绉掓暟 + * @param dateTime 鏃ユ湡 + * @param lateSecond 寤惰繜绉掓暟 + * @return + */ + public 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 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; + } } -- Gitblit v1.8.0