| | |
| | | |
| | | import java.sql.Timestamp; |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.Instant; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.time.ZoneId; |
| | | import java.time.*; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.time.temporal.ChronoUnit; |
| | | import java.time.temporal.TemporalAdjusters; |
| | |
| | | return LocalDate.parse(date); |
| | | } |
| | | /** |
| | | * 两个日期字符串yyyy-MM-dd HH:mm:ss 相差的小时数 |
| | | * @param date1 日期字符串 |
| | | */ |
| | | public static String getDateHoursMinutesSeconds(String date1,String date2) { |
| | | // 定义日期格式 |
| | | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
| | | |
| | | // 解析日期字符串为LocalDateTime对象 |
| | | LocalDateTime dateTime1 = LocalDateTime.parse(date1, formatter); |
| | | LocalDateTime dateTime2 = LocalDateTime.parse(date2, formatter); |
| | | |
| | | // 计算两个日期相差的小时数 |
| | | Duration duration = Duration.between(dateTime1, dateTime2); |
| | | long hours = duration.toHours(); // 获取小时数部分 |
| | | long minutes = duration.toMinutes() % 60; // 获取剩余分钟数部分 |
| | | long seconds = duration.getSeconds() % 60; // 获取剩余秒数部分 |
| | | return hours+"时"+minutes+"分"+seconds+"秒"; |
| | | } |
| | | |
| | | /** |
| | | * 获取LocalDate |
| | | * @param year 年份 |
| | | * @param month 月份 |