mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-20 11:05:18 +08:00
新增 dateUtils 根据传入的日期,获取时间区间中所有的日期
This commit is contained in:
@@ -10,6 +10,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.Instant;
|
||||
@@ -150,6 +151,11 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws ParseException {
|
||||
String startDate = "2023-07-01";
|
||||
String endDate = "2023-07-20";
|
||||
List<String> list = getAllDatesInTheDateRange(startDate, endDate);
|
||||
System.out.println(list);
|
||||
|
||||
// String str = "2023-01-07 11:17:12";
|
||||
// Date date = parseDate(str);
|
||||
// String str1 = parseDateToStr(YYYY_MM_DD_HH_MM_SS, date);
|
||||
@@ -292,6 +298,26 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
|
||||
return localDateTime2Date(localDateTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串转localDate
|
||||
* @param dateStr 格式"yyyy-MM-dd"
|
||||
* @return
|
||||
*/
|
||||
public static LocalDate dateStr2LocalDate(String dateStr) {
|
||||
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
||||
return LocalDate.parse(dateStr, fmt);
|
||||
}
|
||||
|
||||
/**
|
||||
* localDate转dateStr
|
||||
* @param localDate
|
||||
* @return 格式"yyyy-MM-dd"
|
||||
*/
|
||||
public static String localDate2DateStr(LocalDate localDate) {
|
||||
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
||||
return localDate.format(df);
|
||||
}
|
||||
|
||||
/**
|
||||
* Date转LocalDateTime
|
||||
*/
|
||||
@@ -843,4 +869,70 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据传入的日期,获取时间区间中所有的日期
|
||||
*
|
||||
* @param startDate 开始日期
|
||||
* @param endDate 结束日期
|
||||
* @return java.util.List<java.time.LocalDate>
|
||||
* @since 2023-07-20
|
||||
*/
|
||||
public static List<LocalDate> getAllDatesInTheDateRange(LocalDate startDate, LocalDate endDate) {
|
||||
List<LocalDate> localDateList = new ArrayList<>();
|
||||
// 开始时间必须小于结束时间
|
||||
if (startDate.isAfter(endDate)) {
|
||||
return null;
|
||||
}
|
||||
while (startDate.isBefore(endDate)) {
|
||||
localDateList.add(startDate);
|
||||
startDate = startDate.plusDays(1);
|
||||
}
|
||||
localDateList.add(endDate);
|
||||
return localDateList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据传入的日期,获取时间区间中所有的日期
|
||||
* @param startDate 开始日期
|
||||
* @param endDate 结束日期
|
||||
* @return java.util.List<String>
|
||||
*/
|
||||
public static List<String> getAllDatesInTheDateRange(String startDate, String endDate) {
|
||||
// 判断传进来的日期格式
|
||||
if (judgeLegalDate(startDate) || judgeLegalDate(endDate)) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
List<LocalDate> localDateList = getAllDatesInTheDateRange(dateStr2LocalDate(startDate), dateStr2LocalDate(endDate));
|
||||
if (CollectionUtils.isEmpty(localDateList)) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
List<String> resultList = new ArrayList<>();
|
||||
localDateList.forEach(localDate -> {
|
||||
String dateStr = localDate2DateStr(localDate);
|
||||
resultList.add(dateStr);
|
||||
});
|
||||
return resultList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断时间格式 格式必须为“YYYY-MM-dd”
|
||||
* 2004-2-30 是无效的
|
||||
* 2003-2-29 是无效的
|
||||
* @param sDate
|
||||
* @return
|
||||
*/
|
||||
private static boolean judgeLegalDate(String sDate) {
|
||||
int legalLen = 10;
|
||||
if ((sDate == null) || (sDate.length() != legalLen)) {
|
||||
return true;
|
||||
}
|
||||
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
|
||||
try {
|
||||
Date date = formatter.parse(sDate);
|
||||
return !sDate.equals(formatter.format(date));
|
||||
} catch (Exception e) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user