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

60 lines
1.4 KiB
Java
Raw Normal View History

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
}