Преглед на файлове

任务来源:机场

任务描述:日期工具类
develop
wubin преди 1 година
родител
ревизия
28722f8c22
променени са 1 файла, в които са добавени 25 реда и са изтрити 3 реда
  1. +25
    -3
      tuoheng-common/src/main/java/com/tuoheng/common/utils/DateUtils.java

+ 25
- 3
tuoheng-common/src/main/java/com/tuoheng/common/utils/DateUtils.java Целия файл

@@ -10,9 +10,7 @@ import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.*;

/**
* 时间工具类
@@ -234,6 +232,25 @@ public final class DateUtils extends org.apache.commons.lang3.time.DateUtils {
return nextDate + " " + beginDateTime;
}

/**
* 获取日期列表中离当前日期最近的一个日期
* @param dateList
* @return
*/
public static String getLatstDate(List<Date> dateList){

final long now = System.currentTimeMillis();
// Get date closest to "now"
Date closest = Collections.min(dateList, new Comparator<Date>() {
public int compare(Date d1, Date d2) {
long diff1 = Math.abs(d1.getTime() - now);
long diff2 = Math.abs(d2.getTime() - now);
return Long.compare(diff1, diff2);
}
});
return DateUtils.parseDateToStr(YYYY_MM_DD_HH_MM_SS, closest);
}

public static void main(String[] args) {
String beginDate = "2023-05-04 12:12:12";
try {
@@ -242,5 +259,10 @@ public final class DateUtils extends org.apache.commons.lang3.time.DateUtils {
} catch (ParseException e) {
throw new RuntimeException(e);
}
List<Date> list = new ArrayList<>();
list.add(DateUtils.dateTime(YYYY_MM_DD_HH_MM_SS,"2023-05-12 12:00:00"));
list.add(DateUtils.dateTime(YYYY_MM_DD_HH_MM_SS,"2023-06-12 12:00:00"));
list.add(DateUtils.dateTime(YYYY_MM_DD_HH_MM_SS,"2023-07-12 11:00:00"));
System.out.println(getLatstDate(list));
}
}

Loading…
Отказ
Запис