代码示例:
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
public class DateUtil {
public static void main(String[] args) {
long subDays = subDaysByDate("2021-10-01", "2021-11-20");
System.out.println(subDays);
long subDays2 = subDaysByDate2("2021-10-01", "2021-11-20");
System.out.println(subDays2);
long subDays3 = subDaysByDateTime("2021-10-01 10:00:00", "2021-11-20 11:00:00");
System.out.println(subDays3);
}
private static final String DATE_PATTERN = "yyyy-MM-dd";
private static final String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
/**
* 获取两个日期相差的天数 方法一
*
* @param startDate 开始日期的字符串 yyyy-MM-dd
* @param endDate 结束日期的字符串 yyyy-MM-dd
* @return 相差天数,如果解析失败则返回-1
**/
publi