hfxc226 3 years ago
parent
commit
99e2dbce1c

+ 40 - 8
platform-common/src/main/java/com/platform/common/util/DateUtils.java

@@ -394,15 +394,51 @@ public class DateUtils {
         return dateToZoneId(date).atZone(ZONE_ID).toLocalDateTime();
     }
 
+    /**
+     * 相差分钟数
+     * @param startTime
+     * @param endTime
+     * @return
+     */
     public static long getDurationMinutes(LocalDateTime startTime, LocalDateTime  endTime ){
         Duration duration = Duration.between(startTime,endTime);
         return duration.toMinutes();
     }
 
+    /**
+     * 相差分钟数:字符串格式
+     * @param startTime
+     * @param endTime
+     * @return
+     */
     public static String getDurationMinutesStr(LocalDateTime startTime, LocalDateTime  endTime ){
         return String.valueOf(getDurationMinutes(startTime,endTime ));
     }
 
+    /**
+     * 相差小时数
+     * @param startTime
+     * @param endTime
+     * @return
+     */
+    public static double getDurationHours(LocalDateTime startTime, LocalDateTime  endTime ){
+        Duration duration = Duration.between(startTime,endTime);
+        long minitues = duration.toMinutes();
+        double hours = BigDecimalUtil.round(minitues/60.0, 2);
+        return hours;
+    }
+
+    /**
+     * 相差小时数:字符串
+     * @param startTime
+     * @param endTime
+     * @return
+     */
+    public static String getDurationHoursStr(LocalDateTime startTime, LocalDateTime  endTime ){
+        Duration duration = Duration.between(startTime,endTime);
+        return String.valueOf(getDurationHours(startTime,endTime ));
+    }
+
     public static String dateDiff(LocalDateTime end, LocalDateTime now){
         //获取秒数
         long nowSecond = now.toEpochSecond(ZoneOffset.ofHours(0));
@@ -421,17 +457,13 @@ public class DateUtils {
     }
 
     public static void main(String[] args) {
-        //List<Map<String, LocalDateTime>>  monthStartAndEndList = getMonthStartAndEndByYear(2021);
 
-        List<Map<String, LocalDateTime>>  monthStartAndEndList = getMonthStartAndEndByYear(LocalDate.now().withYear(2020).withMonth(2), LocalDate.now().withYear(2021).withMonth(8));
-        for(Map<String, LocalDateTime> map: monthStartAndEndList){
-            System.out.println(map.get("searchStartTimeMonth").toString() + " : " +  map.get("searchEndTimeMonth").toString());
-        }
-        /*LocalDate nowDate = LocalDate.now();
+        LocalDate nowDate = LocalDate.now();
         LocalDateTime time = getFirstDayOfMonth(nowDate);
         System.out.println(time);
-        LocalDateTime time2 = getLastDayOfMonth(nowDate);
-        System.out.println(time2);*/
+        LocalDateTime time2 = time.withHour(12).withMinute(10);
+        System.out.println(time2);
+        System.out.println(getDurationHoursStr(time, time2));
     }
     /**
      * date 转 LocalDate