Commit 773e7cdc by songxiang

增加日期类方法

parent fa4d904a
/** /**
* *
*/ */
package com.pcloud.common.utils; package com.pcloud.common.utils;
import java.time.Duration; import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Date; import java.util.Date;
import com.pcloud.common.utils.string.StringUtil; import com.pcloud.common.utils.string.StringUtil;
/** /**
* @描述:jdk8中新增的日期处理类,更安全、更精确也更明确 * @描述:jdk8中新增的日期处理类,更安全、更精确也更明确
* @作者:songx * @作者:songx
* @创建时间:2017年7月20日,上午9:11:37 @版本:1.0 * @创建时间:2017年7月20日,上午9:11:37 @版本:1.0
*/ */
public class LocalDateUtils { public class LocalDateUtils {
/** /**
* 获取当前的日期字符串(yyyy-MM-dd) * 获取当前的日期字符串(yyyy-MM-dd)
* *
* @return * @return
*/ */
public static String getDateNow() { public static String getDateNow() {
return LocalDate.now().toString(); return LocalDate.now().toString();
} }
/** /**
* 获取当前的日期字符串(yyyy-MM-dd HH:mm:ss) * 获取当前的日期字符串(yyyy-MM-dd HH:mm:ss)
* *
* @return * @return
*/ */
public static String getDateTimeNow() { public static String getDateTimeNow() {
return LocalDateTime.now().withNano(0).toString().replace("T", " "); return LocalDateTime.now().withNano(0).toString().replace("T", " ");
} }
/** /**
* 获取当前的日期字符串(yyyyMMddHHmmssSSS) * 获取当前的日期字符串(yyyyMMddHHmmssSSS)
* *
* @return * @return
*/ */
public static String getYmdhmss() { public static String getYmdhmss() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
return LocalDateTime.now().format(formatter); return LocalDateTime.now().format(formatter);
} }
/** /**
* 时间戳转换成字符串格式的时间 * 时间戳转换成字符串格式的时间
* *
* @param time * @param time
* @return * @return
*/ */
public static String convertToString(long time) { public static String convertToString(long time) {
return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()).toString().replace("T", " "); return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()).toString().replace("T", " ");
} }
/** /**
* 转换成Stirng类型的时间字符串(yyyy-MM-dd HH:mm:ss) * 转换成Stirng类型的时间字符串(yyyy-MM-dd HH:mm:ss)
* *
* @param localDateTime * @param localDateTime
* @return * @return
*/ */
public static String convertToString(LocalDateTime localDateTime) { public static String convertToString(LocalDateTime localDateTime) {
return localDateTime.withNano(0).toString().replace("T", " "); return localDateTime.withNano(0).toString().replace("T", " ");
} }
/** /**
* 获取当前日前多少天以前的日期 * 获取当前日前多少天以前的日期
* *
* @param days * @param days
* @return * @return
*/ */
public static LocalDate getMinusDays(long days) { public static LocalDate getMinusDays(long days) {
LocalDate localDate = LocalDate.now(); LocalDate localDate = LocalDate.now();
return localDate.minusDays(days); return localDate.minusDays(days);
} }
/** /**
* 获取当前日前多少小时以前的日期 * 获取当前日前多少小时以前的日期
* *
* @param minutes * @param minutes
* @return * @return
*/ */
public static LocalDateTime getMinusMinutes(long minutes) { public static LocalDateTime getMinusHours(long hours) {
LocalDateTime localDateTime = LocalDateTime.now(); LocalDateTime localDateTime = LocalDateTime.now();
return localDateTime.minusMinutes(minutes); return localDateTime.minusHours(hours);
} }
/** /**
* 获取指定日前多少天以后的日期 * 获取当前日前多少分钟以前的日期
* *
* @param date * @param minutes
* @param days * @return
* @return */
*/ public static LocalDateTime getMinusMinutes(long minutes) {
public static LocalDateTime getPlusDays(Date date, long days) { LocalDateTime localDateTime = LocalDateTime.now();
LocalDateTime localDateTime = convertDateTime(date); return localDateTime.minusMinutes(minutes);
return localDateTime.plusDays(days); }
}
/**
/** * 获取指定日前多少天以后的日期
* Date转换为localDate *
* * @param date
* @param date * @param days
* @return * @return
*/ */
public static LocalDate convertDate(Date date) { public static LocalDateTime getPlusDays(Date date, long days) {
return convertDateTime(date).toLocalDate(); LocalDateTime localDateTime = convertDateTime(date);
} return localDateTime.plusDays(days);
}
/**
* Date转换为localDateTime /**
* * Date转换为localDate
* @param date *
* @return * @param date
*/ * @return
public static LocalDateTime convertDateTime(Date date) { */
if (date == null) { public static LocalDate convertDate(Date date) {
return null; return convertDateTime(date).toLocalDate();
} }
Instant instant = date.toInstant();
return LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); /**
} * Date转换为localDateTime
*
/** * @param date
* string转换为localDateTime * @return
* */
* @param dateTime public static LocalDateTime convertDateTime(Date date) {
* @return if (date == null) {
*/ return null;
public static LocalDateTime convertDateTime(String dateTime) { }
if (StringUtil.isEmpty(dateTime)) { Instant instant = date.toInstant();
return null; return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
} }
if (!dateTime.contains("T")) {
dateTime = dateTime.replace(" ", "T"); /**
} * string转换为localDateTime
return LocalDateTime.parse(dateTime); *
} * @param dateTime
* @return
/** */
* 判断当前日期是否在指定日前之前,含当前日期 public static LocalDateTime convertDateTime(String dateTime) {
* if (StringUtil.isEmpty(dateTime)) {
* @param date return null;
* @return }
*/ if (!dateTime.contains("T")) {
public static boolean isBefore(Date date) { dateTime = dateTime.replace(" ", "T");
if (date == null) { }
return false; return LocalDateTime.parse(dateTime);
} }
LocalDateTime localDateTime = convertDateTime(date);
return LocalDateTime.now().isBefore(localDateTime); /**
} * 判断当前日期是否在指定日前之前,含当前日期
*
/** * @param date
* 判断当前日期是否在指定日前之后,含当前日期 * @return
* */
* @param date public static boolean isBefore(Date date) {
* @return if (date == null) {
*/ return false;
public static boolean isAfter(Date date) { }
if (date == null) { LocalDateTime localDateTime = convertDateTime(date);
return false; return LocalDateTime.now().isBefore(localDateTime);
} }
LocalDateTime localDateTime = convertDateTime(date);
return LocalDateTime.now().isAfter(localDateTime); /**
} * 判断当前日期是否在指定日前之后,含当前日期
*
/** * @param date
* 判断当前日期是否在指定日前之后,含当前日期 * @return
* */
* @param localDateTime public static boolean isAfter(Date date) {
* @return if (date == null) {
*/ return false;
public static boolean isAfter(LocalDateTime localDateTime) { }
if (localDateTime == null) { LocalDateTime localDateTime = convertDateTime(date);
return false; return LocalDateTime.now().isAfter(localDateTime);
} }
return LocalDateTime.now().isAfter(localDateTime);
} /**
* 判断当前日期是否在指定日前之后,含当前日期
/** *
* 判断第一个日期是否在第二个日前之后 * @param localDateTime
* * @return
* @param firstDateTime */
* @param secondDateTime public static boolean isAfter(LocalDateTime localDateTime) {
* @return if (localDateTime == null) {
*/ return false;
public static boolean isAfter(LocalDateTime firstDateTime, LocalDateTime secondDateTime) { }
if (firstDateTime == null || secondDateTime == null) { return LocalDateTime.now().isAfter(localDateTime);
return false; }
}
return firstDateTime.isAfter(secondDateTime); /**
} * 判断第一个日期是否在第二个日前之后
*
/** * @param firstDateTime
* 判断第一个日期是否在第二个日前之后 * @param secondDateTime
* * @return
* @param firstDateTime */
* @param secondDateTime public static boolean isAfter(LocalDateTime firstDateTime, LocalDateTime secondDateTime) {
* @return if (firstDateTime == null || secondDateTime == null) {
*/ return false;
public static boolean isAfter(String firstDateTime, String secondDateTime) { }
if (firstDateTime == null || secondDateTime == null) { return firstDateTime.isAfter(secondDateTime);
return false; }
}
return convertDateTime(firstDateTime).isAfter(convertDateTime(secondDateTime)); /**
} * 判断第一个日期是否在第二个日前之后
*
/** * @param firstDateTime
* 判断第一个日期是否在第二个日前之后 * @param secondDateTime
* * @return
* @param firstDateTime */
* @param secondDateTime public static boolean isAfter(String firstDateTime, String secondDateTime) {
* @return if (firstDateTime == null || secondDateTime == null) {
*/ return false;
public static boolean isAfter(Date firstDateTime, Date secondDateTime) { }
if (firstDateTime == null || secondDateTime == null) { return convertDateTime(firstDateTime).isAfter(convertDateTime(secondDateTime));
return false; }
}
return convertDateTime(firstDateTime).isAfter(convertDateTime(secondDateTime)); /**
} * 判断第一个日期是否在第二个日前之后
*
/** * @param firstDateTime
* 判断当前日期是否在一定的日期之内,包含前后的两天,例:2017-07-15->[2017-07-01, 2017-07-15] * @param secondDateTime
* * @return
* @param startDate */
* @param endDate public static boolean isAfter(Date firstDateTime, Date secondDateTime) {
* @return if (firstDateTime == null || secondDateTime == null) {
*/ return false;
public static boolean isInIncludeDate(LocalDate startDate, LocalDate endDate) { }
if (startDate == null || endDate == null) { return convertDateTime(firstDateTime).isAfter(convertDateTime(secondDateTime));
return false; }
}
LocalDate nowTime = LocalDate.now(); /**
if (nowTime.isAfter(startDate.plusDays(-1)) && nowTime.isBefore(endDate.plusDays(1))) { * 判断当前日期是否在一定的日期之内,包含前后的两天,例:2017-07-15->[2017-07-01, 2017-07-15]
return true; *
} else { * @param startDate
return false; * @param endDate
} * @return
} */
public static boolean isInIncludeDate(LocalDate startDate, LocalDate endDate) {
/** if (startDate == null || endDate == null) {
* 判断当前日期是否在一定的日期之内,包含前后的两天,例:2017-07-15->[2017-07-01, 2017-07-15] return false;
* }
* @param startDate LocalDate nowTime = LocalDate.now();
* @param endDate if (nowTime.isAfter(startDate.plusDays(-1)) && nowTime.isBefore(endDate.plusDays(1))) {
* @return return true;
*/ } else {
public static boolean isInIncludeDateString(String startDate, String endDate) { return false;
return isInIncludeDate(LocalDate.parse(startDate), LocalDate.parse(endDate)); }
} }
/** /**
* 判断当前日期是否在一定的日期之内,不包含前后的两天,例:2017-07-15->(2017-07-01, 2017-07-15) * 判断当前日期是否在一定的日期之内,包含前后的两天,例:2017-07-15->[2017-07-01, 2017-07-15]
* *
* @param startDate * @param startDate
* @param endDate * @param endDate
* @return * @return
*/ */
public static boolean isInBetweenDate(LocalDate startDate, LocalDate endDate) { public static boolean isInIncludeDateString(String startDate, String endDate) {
if (startDate == null || endDate == null) { return isInIncludeDate(LocalDate.parse(startDate), LocalDate.parse(endDate));
return false; }
}
LocalDate nowTime = LocalDate.now(); /**
if (nowTime.isAfter(startDate) && nowTime.isBefore(endDate)) { * 判断当前日期是否在一定的日期之内,不包含前后的两天,例:2017-07-15->(2017-07-01, 2017-07-15)
return true; *
} else { * @param startDate
return false; * @param endDate
} * @return
} */
public static boolean isInBetweenDate(LocalDate startDate, LocalDate endDate) {
/** if (startDate == null || endDate == null) {
* 判断当前日期是否在一定的日期之内,不包含前后的两天,例:2017-07-15->(2017-07-01, 2017-07-15) return false;
* }
* @param startDate LocalDate nowTime = LocalDate.now();
* @param endDate if (nowTime.isAfter(startDate) && nowTime.isBefore(endDate)) {
* @return return true;
*/ } else {
public static boolean isInBetweenDateString(String startDate, String endDate) { return false;
return isInBetweenDate(LocalDate.parse(startDate), LocalDate.parse(endDate)); }
} }
/** /**
* 判断当前日期是否在一定的日期之内, 例:2017-07-15 12:12:12->(2017-07-01 12:12:12, 2017-07-15 * 判断当前日期是否在一定的日期之内,不包含前后的两天,例:2017-07-15->(2017-07-01, 2017-07-15)
* 12:12:12) *
* * @param startDate
* @param startDateTime * @param endDate
* @param endDateTime * @return
* @return */
*/ public static boolean isInBetweenDateString(String startDate, String endDate) {
public static boolean isInBetweenDateTime(LocalDateTime startDateTime, LocalDateTime endDateTime) { return isInBetweenDate(LocalDate.parse(startDate), LocalDate.parse(endDate));
if (startDateTime == null || endDateTime == null) { }
return false;
} /**
LocalDateTime nowTime = LocalDateTime.now(); * 判断当前日期是否在一定的日期之内, 例:2017-07-15 12:12:12->(2017-07-01 12:12:12, 2017-07-15
if (nowTime.isAfter(startDateTime) && nowTime.isBefore(endDateTime)) { * 12:12:12)
return true; *
} else { * @param startDateTime
return false; * @param endDateTime
} * @return
} */
public static boolean isInBetweenDateTime(LocalDateTime startDateTime, LocalDateTime endDateTime) {
/** if (startDateTime == null || endDateTime == null) {
* 判断当前日期是否在一定的日期之内, 例:2017-07-15 12:12:12->(2017-07-01 12:12:12, 2017-07-15 return false;
* 12:12:12) }
* LocalDateTime nowTime = LocalDateTime.now();
* @param startDateTime if (nowTime.isAfter(startDateTime) && nowTime.isBefore(endDateTime)) {
* @param endDateTime return true;
* @return } else {
*/ return false;
public static boolean isInBetweenDateTime(Date startDateTime, Date endDateTime) { }
return isInBetweenDateTime(convertDateTime(startDateTime), convertDateTime(endDateTime)); }
}
/**
/** * 判断当前日期是否在一定的日期之内, 例:2017-07-15 12:12:12->(2017-07-01 12:12:12, 2017-07-15
* 判断当前日期是否在一定的日期之内, 例:2017-07-15 12:12:12->(2017-07-01 12:12:12, 2017-07-15 * 12:12:12)
* 12:12:12) *
* * @param startDateTime
* @param startDateTime * @param endDateTime
* @param endDateTime * @return
* @return */
*/ public static boolean isInBetweenDateTime(Date startDateTime, Date endDateTime) {
public static boolean isInBetweenDateTimeString(String startDateTime, String endDateTime) { return isInBetweenDateTime(convertDateTime(startDateTime), convertDateTime(endDateTime));
return isInBetweenDateTime(LocalDateTime.parse(startDateTime), LocalDateTime.parse(endDateTime)); }
}
/**
/** * 判断当前日期是否在一定的日期之内, 例:2017-07-15 12:12:12->(2017-07-01 12:12:12, 2017-07-15
* 获取两个时间差,天,时,分 * 12:12:12)
* *
* @param stratDateTime * @param startDateTime
* @param endDateTime * @param endDateTime
* @return * @return
*/ */
public static long[] getDatePoor(LocalDateTime stratDateTime, LocalDateTime endDateTime) { public static boolean isInBetweenDateTimeString(String startDateTime, String endDateTime) {
if (stratDateTime == null || stratDateTime == null) { return isInBetweenDateTime(LocalDateTime.parse(startDateTime), LocalDateTime.parse(endDateTime));
return null; }
}
Duration duration = Duration.between(stratDateTime, endDateTime); /**
// 计算差多少天 * 获取两个时间差,天,时,分
long day = duration.toDays(); *
// 计算差多少小时 * @param stratDateTime
long hour = duration.toHours() - day * 24; * @param endDateTime
// 计算差多少分钟 * @return
long min = duration.toMinutes() - duration.toHours() * 60; */
return new long[] { day, hour, min }; public static long[] getDatePoor(LocalDateTime stratDateTime, LocalDateTime endDateTime) {
} if (stratDateTime == null || stratDateTime == null) {
return null;
/** }
* 获取指定时间与当前日期相差多少小时 Duration duration = Duration.between(stratDateTime, endDateTime);
* // 计算差多少天
* @param date long day = duration.toDays();
* @return // 计算差多少小时
*/ long hour = duration.toHours() - day * 24;
public static long getHourPoor(Date date) { // 计算差多少分钟
if (date == null) { long min = duration.toMinutes() - duration.toHours() * 60;
return -1; return new long[] { day, hour, min };
} }
return getHourPoor(convertDateTime(date));
} /**
* 获取指定时间与当前日期相差多少小时
/** *
* 获取指定时间与当前日期相差多少小时 * @param date
* * @return
* @param dateTime */
* @return public static long getHourPoor(Date date) {
*/ if (date == null) {
public static long getHourPoor(LocalDateTime dateTime) { return -1;
if (dateTime == null) { }
return -1; return getHourPoor(convertDateTime(date));
} }
Duration duration = Duration.between(dateTime, LocalDateTime.now());
return duration.toHours(); /**
} * 获取指定时间与当前日期相差多少小时
*
/** * @param dateTime
* 获取指定时间与当前日期相差多少天 * @return
* */
* @param date public static long getHourPoor(LocalDateTime dateTime) {
* @return if (dateTime == null) {
*/ return -1;
public static long getDayPoor(Date date) { }
if (date == null) { Duration duration = Duration.between(dateTime, LocalDateTime.now());
return -1; return duration.toHours();
} }
return getDayPoor(convertDateTime(date));
} /**
* 获取指定时间与当前日期相差多少天
/** *
* 获取指定时间与当前日期相差多少天 * @param date
* * @return
* @param dateTime */
* @return public static long getDayPoor(Date date) {
*/ if (date == null) {
public static long getDayPoor(LocalDateTime dateTime) { return -1;
if (dateTime == null) { }
return -1; return getDayPoor(convertDateTime(date));
} }
Duration duration = Duration.between(dateTime, LocalDateTime.now());
return duration.toDays(); /**
} * 获取指定时间与当前日期相差多少天
*
} * @param dateTime
* @return
*/
public static long getDayPoor(LocalDateTime dateTime) {
if (dateTime == null) {
return -1;
}
Duration duration = Duration.between(dateTime, LocalDateTime.now());
return duration.toDays();
}
}
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