From bc8074c56893eac3eb5002dcefeacd644d827a29 Mon Sep 17 00:00:00 2001 From: zhanghua <314079846@qq.com> Date: 星期日, 21 一月 2024 20:54:27 +0800 Subject: [PATCH] 优化 --- ycl-platform/src/main/java/com/ycl/common/util/DateUtil.java | 384 +++++++++++++++++++++++++++--------------------------- 1 files changed, 194 insertions(+), 190 deletions(-) diff --git a/ycl-platform/src/main/java/com/ycl/common/util/DateUtil.java b/ycl-platform/src/main/java/com/ycl/common/util/DateUtil.java index db296c7..c392d05 100644 --- a/ycl-platform/src/main/java/com/ycl/common/util/DateUtil.java +++ b/ycl-platform/src/main/java/com/ycl/common/util/DateUtil.java @@ -12,219 +12,223 @@ public class DateUtil { - /** - * 鑾峰彇涓ゆ棩鏈熶箣闂撮棿闅旂殑澶╂暟 - * - * @param smdate - * 杈冨皬鐨勬椂闂� - * @param bdate - * 杈冨ぇ鐨勬椂闂� - * @return 鐩稿樊澶╂暟 - */ - public static int daysBetween(Date smdate, Date bdate) { - long between_days = 0; - try { - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); - smdate = sdf.parse(sdf.format(smdate)); - bdate = sdf.parse(sdf.format(bdate)); - Calendar cal = Calendar.getInstance(); - cal.setTime(smdate); - long time1 = cal.getTimeInMillis(); - cal.setTime(bdate); - long time2 = cal.getTimeInMillis(); - between_days = (time2 - time1) / (1000 * 3600 * 24); - } catch (ParseException e) { - e.printStackTrace(); - } + /** + * 鑾峰彇涓ゆ棩鏈熶箣闂撮棿闅旂殑澶╂暟 + * + * @param smdate 杈冨皬鐨勬椂闂� + * @param bdate 杈冨ぇ鐨勬椂闂� + * @return 鐩稿樊澶╂暟 + */ + public static int daysBetween(Date smdate, Date bdate) { + long between_days = 0; + try { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + smdate = sdf.parse(sdf.format(smdate)); + bdate = sdf.parse(sdf.format(bdate)); + Calendar cal = Calendar.getInstance(); + cal.setTime(smdate); + long time1 = cal.getTimeInMillis(); + cal.setTime(bdate); + long time2 = cal.getTimeInMillis(); + between_days = (time2 - time1) / (1000 * 3600 * 24); + } catch (ParseException e) { + e.printStackTrace(); + } - return Integer.parseInt(String.valueOf(between_days)); - } + return Integer.parseInt(String.valueOf(between_days)); + } - public static boolean isBetween(Date date, Date startDate, Date endDate) { - - try { + public static boolean isBetween(Date date, Date startDate, Date endDate) { - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - startDate = sdf.parse(sdf.format(startDate)); - endDate = sdf.parse(sdf.format(endDate)); - date = sdf.parse(sdf.format(date)); + try { - Calendar cal = Calendar.getInstance(); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + startDate = sdf.parse(sdf.format(startDate)); + endDate = sdf.parse(sdf.format(endDate)); + date = sdf.parse(sdf.format(date)); - cal.setTime(date); - long time0 = cal.getTimeInMillis(); - cal.setTime(startDate); - long time1 = cal.getTimeInMillis(); - cal.setTime(endDate); - long time2 = cal.getTimeInMillis(); + Calendar cal = Calendar.getInstance(); - if (time0 > time1 && time0 <= time2) - return true; + cal.setTime(date); + long time0 = cal.getTimeInMillis(); + cal.setTime(startDate); + long time1 = cal.getTimeInMillis(); + cal.setTime(endDate); + long time2 = cal.getTimeInMillis(); - } catch (ParseException e) { - e.printStackTrace(); - } - return false; + if (time0 > time1 && time0 <= time2) + return true; - } + } catch (ParseException e) { + e.printStackTrace(); + } + return false; - public static long DifferMillis(Date sDate, Date eDate) { - try { - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - sDate = sdf.parse(sdf.format(sDate)); - eDate = sdf.parse(sdf.format(eDate)); - Calendar cal = Calendar.getInstance(); - cal.setTime(sDate); - long time1 = cal.getTimeInMillis(); - cal.setTime(eDate); - long time2 = cal.getTimeInMillis(); - return Math.abs(time1 - time2); - } catch (ParseException e) { - e.printStackTrace(); - } + } - return -1L; - } + public static long DifferMillis(Date sDate, Date eDate) { + try { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + sDate = sdf.parse(sdf.format(sDate)); + eDate = sdf.parse(sdf.format(eDate)); + Calendar cal = Calendar.getInstance(); + cal.setTime(sDate); + long time1 = cal.getTimeInMillis(); + cal.setTime(eDate); + long time2 = cal.getTimeInMillis(); + return Math.abs(time1 - time2); + } catch (ParseException e) { + e.printStackTrace(); + } - public static long DifferSeconds(Date sDate, Date eDate) { + return -1L; + } - return DifferMillis(sDate, eDate) / 1000; - } + public static long DifferSeconds(Date sDate, Date eDate) { - /** - * 鐩稿樊鍒嗛挓鏁� - * - * @param sDate - * @param eDate - * @return - */ - public static long DifferMinutes(Date sDate, Date eDate) { + return DifferMillis(sDate, eDate) / 1000; + } - return Long.parseLong(String.valueOf(DifferMillis(sDate, eDate) - / (1000 * 60))); - } + /** + * 鐩稿樊鍒嗛挓鏁� + * + * @param sDate + * @param eDate + * @return + */ + public static long DifferMinutes(Date sDate, Date eDate) { - public static String getNowDate(String format) { - return new SimpleDateFormat(format).format(new Date()); - } + return Long.parseLong(String.valueOf(DifferMillis(sDate, eDate) + / (1000 * 60))); + } - public static String getDateString(Date date, String format) { - return new SimpleDateFormat(format).format(date); - } + public static String getNowDate(String format) { + return new SimpleDateFormat(format).format(new Date()); + } - public static Date parse(String date, String format) { + public static String getDateString(Date date, String format) { + return new SimpleDateFormat(format).format(date); + } - try { - return new SimpleDateFormat(format).parse(date); - } catch (ParseException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return null; - } + public static Date parse(String date, String format) { - public static boolean nowTimeBetween(String beginTimeStr, String endTimeStr) - throws ParseException { - SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss"); - Long beginTime = dateFormat.parse(beginTimeStr).getTime(); - Long endTime = dateFormat.parse(endTimeStr).getTime(); - Long nowTime = dateFormat.parse(dateFormat.format(new Date())) - .getTime(); - if (nowTime >= beginTime && nowTime <= endTime) { - return true; - } - return false; - } + try { + return new SimpleDateFormat(format).parse(date); + } catch (ParseException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } - @SuppressWarnings("static-access") - public static Date addDate(Date date, int days) { - Calendar calendar = new GregorianCalendar(); - calendar.setTime(date); - calendar.add(calendar.DATE, days);// 鎶婃棩鏈熷線鍚庡鍔犱竴澶�.鏁存暟寰�鍚庢帹,璐熸暟寰�鍓嶇Щ鍔� - return calendar.getTime(); // 杩欎釜鏃堕棿灏辨槸鏃ユ湡寰�鍚庢帹涓�澶╃殑缁撴灉 - } + public static boolean nowTimeBetween(String beginTimeStr, String endTimeStr) + throws ParseException { + SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss"); + Long beginTime = dateFormat.parse(beginTimeStr).getTime(); + Long endTime = dateFormat.parse(endTimeStr).getTime(); + Long nowTime = dateFormat.parse(dateFormat.format(new Date())) + .getTime(); + if (nowTime >= beginTime && nowTime <= endTime) { + return true; + } + return false; + } - @SuppressWarnings("static-access") - public static Date addHour(Date date, int hour) { - Calendar calendar = new GregorianCalendar(); - calendar.setTime(date); - calendar.add(calendar.HOUR, hour);// 鎶婃棩鏈熷線鍚庡鍔犱竴澶�.鏁存暟寰�鍚庢帹,璐熸暟寰�鍓嶇Щ鍔� - return calendar.getTime(); // 杩欎釜鏃堕棿灏辨槸鏃ユ湡寰�鍚庢帹涓�澶╃殑缁撴灉 - } + @SuppressWarnings("static-access") + public static Date addDate(Date date, int days) { + Calendar calendar = new GregorianCalendar(); + calendar.setTime(date); + calendar.add(calendar.DATE, days);// 鎶婃棩鏈熷線鍚庡鍔犱竴澶�.鏁存暟寰�鍚庢帹,璐熸暟寰�鍓嶇Щ鍔� + return calendar.getTime(); // 杩欎釜鏃堕棿灏辨槸鏃ユ湡寰�鍚庢帹涓�澶╃殑缁撴灉 + } - @SuppressWarnings("static-access") - public static Date addMinute(Date date, int minute) { - Calendar calendar = new GregorianCalendar(); - calendar.setTime(date); - calendar.add(calendar.MINUTE, minute);// 鎶婃棩鏈熷線鍚庡鍔犱竴澶�.鏁存暟寰�鍚庢帹,璐熸暟寰�鍓嶇Щ鍔� - return calendar.getTime(); // 杩欎釜鏃堕棿灏辨槸鏃ユ湡寰�鍚庢帹涓�澶╃殑缁撴灉 - } + @SuppressWarnings("static-access") + public static Date addHour(Date date, int hour) { + Calendar calendar = new GregorianCalendar(); + calendar.setTime(date); + calendar.add(calendar.HOUR, hour);// 鎶婃棩鏈熷線鍚庡鍔犱竴澶�.鏁存暟寰�鍚庢帹,璐熸暟寰�鍓嶇Щ鍔� + return calendar.getTime(); // 杩欎釜鏃堕棿灏辨槸鏃ユ湡寰�鍚庢帹涓�澶╃殑缁撴灉 + } - @SuppressWarnings("static-access") - public static Date addSecond(Date date, int second) { - Calendar calendar = new GregorianCalendar(); - calendar.setTime(date); - calendar.add(calendar.SECOND, second);// 鎶婃棩鏈熷線鍚庡鍔犱竴澶�.鏁存暟寰�鍚庢帹,璐熸暟寰�鍓嶇Щ鍔� - return calendar.getTime(); // 杩欎釜鏃堕棿灏辨槸鏃ユ湡寰�鍚庢帹涓�澶╃殑缁撴灉 - } + @SuppressWarnings("static-access") + public static Date addMinute(Date date, int minute) { + Calendar calendar = new GregorianCalendar(); + calendar.setTime(date); + calendar.add(calendar.MINUTE, minute);// 鎶婃棩鏈熷線鍚庡鍔犱竴澶�.鏁存暟寰�鍚庢帹,璐熸暟寰�鍓嶇Щ鍔� + return calendar.getTime(); // 杩欎釜鏃堕棿灏辨槸鏃ユ湡寰�鍚庢帹涓�澶╃殑缁撴灉 + } - /** - * 缁熻涓や釜鏃堕棿鐨勬椂闂村樊 - * two-one - * 鐩稿樊鍑犵鍑犳绉� - */ - public static String getDistanceDateTime(Date one, Date two) { - long day = 0;//澶╂暟宸� - long hour = 0;//灏忔椂鏁板樊 - long min = 0;//鍒嗛挓鏁板樊 - long second=0;//绉掓暟宸� - long diff=0 ;//姣宸� - String result = ""; - final Calendar c = Calendar.getInstance(); - c.setTimeZone(TimeZone.getTimeZone("GMT+8:00")); - c.setTime(one); - long time1 = one.getTime(); - long time2 = two.getTime(); - diff = time2 - time1; - day = diff / (24 * 60 * 60 * 1000); - hour = (diff / (60 * 60 * 1000) - day * 24); - min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60); - second = diff/1000; - // System.out.println("day="+day+" hour="+hour+" min="+min+" ss="+second%60+" SSS="+diff%1000); - String daystr = day%30+"澶�"; - String hourStr = hour%24+"灏忔椂"; - String minStr = min%60+"鍒�"; - String secondStr = second%60+"绉�"; - if (day!=0){ - result = result + daystr; - } - if (hour!=0){ - result = result + hourStr; - } - if (min!=0){ - result = result + minStr; - } - if (second!=0){ - result = result + secondStr; - } - return result; - } - public static Date fromLocalDateTime(LocalDateTime ldt){ - ZoneId zoneId = ZoneId.systemDefault(); - ZonedDateTime zdt = ldt.atZone(zoneId); - Date date = Date.from(zdt.toInstant()); - return date ; - } - public static void main(String[] args) throws ParseException { + @SuppressWarnings("static-access") + public static Date addSecond(Date date, int second) { + Calendar calendar = new GregorianCalendar(); + calendar.setTime(date); + calendar.add(calendar.SECOND, second);// 鎶婃棩鏈熷線鍚庡鍔犱竴澶�.鏁存暟寰�鍚庢帹,璐熸暟寰�鍓嶇Щ鍔� + return calendar.getTime(); // 杩欎釜鏃堕棿灏辨槸鏃ユ湡寰�鍚庢帹涓�澶╃殑缁撴灉 + } - Calendar calendar = Calendar.getInstance(); - calendar.add(Calendar.DATE,-1); - calendar.add(Calendar.HOUR,-6); - calendar.add(Calendar.MINUTE,-5); - calendar.add(Calendar.SECOND,-4); - calendar.add(Calendar.MILLISECOND, -30); - Date date = calendar.getTime(); - Date today = new Date(); - // System.out.println(getDistanceDateTime(date,today)); - } + /** + * 缁熻涓や釜鏃堕棿鐨勬椂闂村樊 + * two-one + * 鐩稿樊鍑犵鍑犳绉� + */ + public static String getDistanceDateTime(Date one, Date two) { + long day = 0;//澶╂暟宸� + long hour = 0;//灏忔椂鏁板樊 + long min = 0;//鍒嗛挓鏁板樊 + long second = 0;//绉掓暟宸� + long diff = 0;//姣宸� + String result = ""; + final Calendar c = Calendar.getInstance(); + c.setTimeZone(TimeZone.getTimeZone("GMT+8:00")); + c.setTime(one); + long time1 = one.getTime(); + long time2 = two.getTime(); + diff = time2 - time1; + day = diff / (24 * 60 * 60 * 1000); + hour = (diff / (60 * 60 * 1000) - day * 24); + min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60); + second = diff / 1000; + // System.out.println("day="+day+" hour="+hour+" min="+min+" ss="+second%60+" SSS="+diff%1000); + String daystr = day % 30 + "澶�"; + String hourStr = hour % 24 + "灏忔椂"; + String minStr = min % 60 + "鍒�"; + String secondStr = second % 60 + "绉�"; + if (day != 0) { + result = result + daystr; + } + if (hour != 0) { + result = result + hourStr; + } + if (min != 0) { + if (day == 0) { + result = result + minStr; + } + } + if (second != 0) { + if (day == 0 && hour == 0) { + result = result + secondStr; + } + } + return result; + } + + public static Date fromLocalDateTime(LocalDateTime ldt) { + ZoneId zoneId = ZoneId.systemDefault(); + ZonedDateTime zdt = ldt.atZone(zoneId); + Date date = Date.from(zdt.toInstant()); + return date; + } + + public static void main(String[] args) throws ParseException { + + Calendar calendar = Calendar.getInstance(); + calendar.add(Calendar.DATE, -1); + calendar.add(Calendar.HOUR, -6); + calendar.add(Calendar.MINUTE, -5); + calendar.add(Calendar.SECOND, -4); + calendar.add(Calendar.MILLISECOND, -30); + Date date = calendar.getTime(); + Date today = new Date(); + // System.out.println(getDistanceDateTime(date,today)); + } } \ No newline at end of file -- Gitblit v1.8.0