driver-server/src/main/java/com/jwl/driver/server/util/DateTimeUtil.java

60 lines
1.4 KiB
Java

package com.jwl.driver.server.util;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
/**
* @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);
}
/**
* 计算两个时间之间间隔的天数
* @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);
}
}