2023-08-11 11:51:09 +08:00
|
|
|
package com.jwl.driver.server.util;
|
|
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
import java.time.format.DateTimeFormatter;
|
2023-08-19 16:28:44 +08:00
|
|
|
import java.time.temporal.ChronoUnit;
|
2023-08-11 11:51:09 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @Author: yangshuang
|
|
|
|
* @Description:
|
|
|
|
* @Date: 2021/2/23 15:23
|
|
|
|
* @Version: 1.0
|
|
|
|
*/
|
|
|
|
public class DateTimeUtil {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 时间转字符串
|
|
|
|
* @param dateTime
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static String dateTime2Str(LocalDateTime dateTime, DateTimeFormatter df) {
|
|
|
|
return df.format(dateTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 字符串转时间
|
|
|
|
* @param str
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static LocalDateTime str2DateTime(String str, DateTimeFormatter df) {
|
|
|
|
return LocalDateTime.parse(str, df);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取当前时间字符串
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static String getCurrentStr(DateTimeFormatter df) {
|
|
|
|
return dateTime2Str(LocalDateTime.now(), df);
|
|
|
|
}
|
2023-08-19 16:28:44 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 计算两个时间之间间隔的天数
|
|
|
|
* @param startDate
|
|
|
|
* @param endDate
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static long getIntervalDays(LocalDateTime startDate, LocalDateTime endDate) {
|
|
|
|
return ChronoUnit.DAYS.between(startDate, endDate);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
LocalDateTime date1 = LocalDateTime.of(2023, 4, 18,0,0,0);
|
|
|
|
LocalDateTime date2 = LocalDateTime.of(2023, 4, 19,23,59,59);
|
|
|
|
long between = ChronoUnit.DAYS.between(date1, date2);
|
|
|
|
System.out.println(between);
|
|
|
|
|
|
|
|
}
|
2023-08-11 11:51:09 +08:00
|
|
|
}
|