Commit 7662c346 by songxiang

增加时间工具类

parent ba0bf269
......@@ -9,7 +9,13 @@ import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.pcloud.common.utils.string.StringUtil;
......@@ -116,9 +122,23 @@ public class LocalDateUtils {
}
/**
* 获取指定日前多少天以前的日期
*
* @param date
* @param days
* @return
*/
public static LocalDate getMinusDays(LocalDate date, long days) {
if (date == null) {
throw new IllegalArgumentException("date is null");
}
return date.minusDays(days);
}
/**
* 获取当前日前多少小时以前的日期
*
* @param minutes
* @param hours
* @return
*/
public static LocalDateTime getMinusHours(long hours) {
......@@ -150,13 +170,30 @@ public class LocalDateUtils {
}
/**
* String转换为localDate
*
* @param date
* @return
*/
public static LocalDate convertDate(String date) {
if (StringUtil.isEmpty(date)) {
return null;
}
return LocalDate.parse(date);
}
/**
* Date转换为localDate
*
* @param date
* @return
*/
public static LocalDate convertDate(Date date) {
return convertDateTime(date).toLocalDate();
LocalDateTime localDateTime = convertDateTime(date);
if (localDateTime == null) {
return null;
}
return localDateTime.toLocalDate();
}
/**
......@@ -394,7 +431,7 @@ public class LocalDateUtils {
long hour = duration.toHours() - day * 24;
// 计算差多少分钟
long min = duration.toMinutes() - duration.toHours() * 60;
return new long[] { day, hour, min };
return new long[]{day, hour, min};
}
/**
......@@ -445,10 +482,109 @@ public class LocalDateUtils {
*/
public static long getDayPoor(LocalDateTime dateTime) {
if (dateTime == null) {
return -1;
throw new IllegalArgumentException("dateTime is null");
}
Duration duration = Duration.between(dateTime, LocalDateTime.now());
return duration.toDays();
}
/**
* 获取指定时间相差多少天
*
* @param startDate 格式:2017-01-01
* @param endDate 格式:2017-02-01
* @return
*/
public static long getDayPoor(String startDate, String endDate) {
if (StringUtil.isEmpty(startDate) || StringUtil.isEmpty(endDate)) {
throw new IllegalArgumentException("startDate or endDate is null");
}
return getDayPoor(convertDate(startDate), convertDate(endDate));
}
/**
* 获取指定时间相差多少天
*
* @param startDate
* @param endDate
* @return
*/
public static long getDayPoor(LocalDate startDate, LocalDate endDate) {
if (startDate == null || endDate == null) {
throw new IllegalArgumentException("startDate or endDate is null");
}
return startDate.until(endDate, ChronoUnit.DAYS);
}
/**
* 获取月份
*
* @param dateTime 时间格式:2019-01-01 00:00:00
* @return
*/
public static int getMonth(String dateTime) {
if (StringUtil.isEmpty(dateTime)) {
return 0;
}
return convertDateTime(dateTime).getMonthValue();
}
/**
* 获取当月第一天
*
* @param date
* @return
*/
public static LocalDate firstDay(LocalDate date) {
if (date == null) {
throw new IllegalArgumentException("date is null");
}
return date.with(TemporalAdjusters.firstDayOfMonth());
}
/**
* 获取当月最后一天
*
* @param date
* @return
*/
public static LocalDate lastDay(LocalDate date) {
if (date == null) {
throw new IllegalArgumentException("date is null");
}
return date.with(TemporalAdjusters.lastDayOfMonth());
}
/**
* 获取指定日期和天数以前的日期时间集合(不含指定日期)
*
* @param date
* @return
*/
public static List<LocalDate> getBeforeDays(LocalDate date, long days) {
if (date == null) {
throw new IllegalArgumentException("date is null");
}
List<LocalDate> localDates = new ArrayList<>();
for (long i = days; i > 0; i--) {
localDates.add(getMinusDays(date, i));
}
return localDates;
}
/**
* 获取指定日期之间的日期集合(含指定日期)
*
* @param startdate
* @param endDate
* @return
*/
public static List<LocalDate> getDates(LocalDate startdate, LocalDate endDate) {
if (startdate == null || endDate == null) {
throw new IllegalArgumentException("date is null");
}
return Stream.iterate(startdate, date -> date.plusDays(1))
.limit(ChronoUnit.DAYS.between(startdate, endDate) + 1).collect(Collectors.toList());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment