public classDateTimeUtil {/*** 获取当前时间时间戳(long)
*@return
*/
public static longcurrentTimeMillis() {returnSystem.currentTimeMillis();
}/*** 获取当前日期(yyyy-MM-dd)
*@return
*/
public staticLocalDate currentLocalDate() {returnLocalDate.now();
}/*** 获取当前时间(HH:mm:ss.SSS)
*@return
*/
public staticLocalTime currentLocalTime() {returnLocalTime.now();
}/*** 获取当前日期时间(yyyy-MM-dd'T'HH:mm:ss.SSS)
*@return
*/
public staticLocalDateTime currentLocalDateTime() {returnLocalDateTime.now();
}/*** 获取当前日期字符串(yyyy-MM-dd)
*@return
*/
public staticString getCurrentDateStr() {returnDateTimeFormatter.ofPattern(TimeFormatter.DATE_FORMATTER).format(currentLocalDateTime());
}/*** 获取当前时间字符串(HH:mm:ss)
*@return
*/
public staticString getCurrentTimeStr() {returnDateTimeFormatter.ofPattern(TimeFormatter.TIME_FORMATTER).format(currentLocalDateTime());
}/*** 获取当前日期时间字符串(yyyy-MM-dd HH:mm:ss)
*@return
*/
public staticString getCurrentDateTimeStr() {returnDateTimeFormatter.ofPattern(TimeFormatter.DATETIME_FORMATTER).format(currentLocalDateTime());
}/*** 将时间字符串转为自定义时间格式的LocalDateTime
*@paramtime 需要转化的时间字符串
*@paramformat 自定义的时间格式
*@return
*/
public staticLocalDateTime convertStringToLocalDateTime(String time, String format) {returnLocalDateTime.parse(time,DateTimeFormatter.ofPattern(format));
}/*** 将LocalDateTime转为自定义的时间格式的字符串
*@paramlocalDateTime 需要转化的LocalDateTime
*@paramformat 自定义的时间格式
*@return
*/
public staticString convertLocalDateTimeToString(LocalDateTime localDateTime, String format) {returnlocalDateTime.format(DateTimeFormatter.ofPattern(format));
}/*** 将long类型的timestamp转为LocalDateTime
*@paramtimestamp
*@return
*/
public static LocalDateTime convertTimestampToLocalDateTime(longtimestamp) {returnLocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp),ZoneId.systemDefault());
}/*** 将LocalDateTime转为long类型的timestamp
*@paramlocalDateTime
*@return
*/
public static longconvertLocalDateTimeToTimestamp(LocalDateTime localDateTime) {returnlocalDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}/*** 获取LocalDateTime的最大时间的字符串格式(yyyy-MM-dd HH:mm:ss)
*@paramlocalDateTime
*@return
*/
public staticString getMaxDateTime(LocalDateTime localDateTime) {returnconvertLocalDateTimeToString(localDateTime.with(LocalTime.MAX),TimeFormatter.DATETIME_FORMATTER);
}/*** 获取LocalDateTime的最小时间的字符串格式(yyyy-MM-dd HH:mm:ss)
*@paramlocalDateTime
*@return
*/
public staticString getMinDateTime(LocalDateTime localDateTime) {returnconvertLocalDateTimeToString(localDateTime.with(LocalTime.MIN),TimeFormatter.DATETIME_FORMATTER);
}/*** 获取LocalDate的最大时间的字符串格式(yyyy-MM-dd HH:mm:ss)
*@paramlocalDate
*@return
*/
public staticString getMaxDateTime(LocalDate localDate) {returnconvertLocalDateTimeToString(localDate.atTime(LocalTime.MAX),TimeFormatter.DATETIME_FORMATTER);
}/*** 获取LocalDate的最小时间的字符串格式(yyyy-MM-dd HH:mm:ss)
*@paramlocalDate
*@return
*/
public staticString getMinDateTime(LocalDate localDate) {returnconvertLocalDateTimeToString(localDate.atTime(LocalTime.MIN),TimeFormatter.DATETIME_FORMATTER);
}
}