| | |
| | | package com.ruoyi.common.core.utils; |
| | | |
| | | import org.apache.commons.lang3.time.DateFormatUtils; |
| | | |
| | | import java.lang.management.ManagementFactory; |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.time.LocalTime; |
| | | import java.time.ZoneId; |
| | | import java.time.ZonedDateTime; |
| | | import java.time.*; |
| | | import java.util.Date; |
| | | import org.apache.commons.lang3.time.DateFormatUtils; |
| | | |
| | | /** |
| | | * 时间工具类 |
| | |
| | | "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", |
| | | "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"}; |
| | | |
| | | public static void main(String[] args) { |
| | | System.out.println(getNowDate()); |
| | | public static void main(String[] args) throws ParseException { |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | Date start = sdf.parse("2021-10-01 10:00:00"); |
| | | Date end = sdf.parse("2021-10-01 12:30:00"); |
| | | String time = formatDuration(start,end); |
| | | System.out.println(time); |
| | | } |
| | | |
| | | /** |
| | |
| | | ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault()); |
| | | return Date.from(zdt.toInstant()); |
| | | } |
| | | |
| | | public static String formatDuration(Date start, Date end) { |
| | | long duration = end.getTime() - start.getTime(); |
| | | long days = duration / (24 * 60 * 60 * 1000); |
| | | long hours = (duration % (24 * 60 * 60 * 1000)) / (60 * 60 * 1000); |
| | | long minutes = (duration % (60 * 60 * 1000)) / (60 * 1000); |
| | | long seconds = (duration % (60 * 1000)) / 1000; |
| | | StringBuilder sb = new StringBuilder(); |
| | | if (days > 0) { |
| | | sb.append(days).append("天"); |
| | | } |
| | | if (hours > 0) { |
| | | sb.append(hours).append("小时"); |
| | | } |
| | | if (minutes > 0) { |
| | | sb.append(minutes).append("分"); |
| | | } |
| | | if (seconds > 0) { |
| | | sb.append(seconds).append("秒"); |
| | | } |
| | | return sb.toString(); |
| | | } |
| | | } |