From f610e6e7b7451efcbccefba881c5128973dcc94c Mon Sep 17 00:00:00 2001 From: fwq <582742538@qq.com> Date: 星期一, 11 十二月 2023 10:41:37 +0800 Subject: [PATCH] 时间工具类优化 --- src/main/java/com/hx/util/DateUtil.java | 40 +++++++++++++++++++++++++++++++++++++++- 1 files changed, 39 insertions(+), 1 deletions(-) diff --git a/src/main/java/com/hx/util/DateUtil.java b/src/main/java/com/hx/util/DateUtil.java index 361a86c..4648efa 100644 --- a/src/main/java/com/hx/util/DateUtil.java +++ b/src/main/java/com/hx/util/DateUtil.java @@ -549,6 +549,7 @@ calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); + calendar.set(Calendar.MILLISECOND,0); return calendar.getTime(); } @@ -581,6 +582,7 @@ calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); + calendar.set(Calendar.MILLISECOND,999); return calendar.getTime(); } @@ -613,13 +615,14 @@ calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); + calendar.set(Calendar.MILLISECOND,0); return calendar.getTime(); } /** * 鑾峰彇褰撳墠骞寸殑缁撴潫鏃堕棿 * @param time 鏃堕棿 - * @return 杩斿洖鏃堕棿 鏍煎紡yyyy-MM-dd 00:00:00 + * @return 杩斿洖鏃堕棿 鏍煎紡yyyy-MM-dd 23:59:59 */ public static Date getYearEnd(Date time) { Calendar calendar = Calendar.getInstance(); @@ -629,6 +632,7 @@ calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); + calendar.set(Calendar.MILLISECOND,999); return calendar.getTime(); } @@ -668,6 +672,7 @@ calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); + calendar.set(Calendar.MILLISECOND, 0); return calendar.getTime(); } @@ -680,6 +685,7 @@ calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); + calendar.set(Calendar.MILLISECOND, 999); return calendar.getTime(); } @@ -813,4 +819,36 @@ } return age; } + + /** + * 鑾峰彇鏌愬ぉ缁撴潫绉掓暟 + * @param dateTime 鏃ユ湡 + * @param lateSecond 寤惰繜绉掓暟 + * @return + */ + public static 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 static 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