|
|
@@ -14,18 +14,13 @@ import cn.hutool.core.util.StrUtil;
|
|
|
|
|
|
import java.text.DateFormat;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
+
|
|
|
import java.time.Instant;
|
|
|
-import java.time.LocalDate;
|
|
|
import java.time.LocalDateTime;
|
|
|
-import java.time.LocalTime;
|
|
|
-import java.time.OffsetDateTime;
|
|
|
-import java.time.OffsetTime;
|
|
|
import java.time.Year;
|
|
|
-import java.time.ZoneId;
|
|
|
-import java.time.ZonedDateTime;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
-import java.time.format.DateTimeParseException;
|
|
|
import java.time.temporal.TemporalAccessor;
|
|
|
+
|
|
|
import java.util.Calendar;
|
|
|
import java.util.Date;
|
|
|
import java.util.GregorianCalendar;
|
|
|
@@ -498,11 +493,7 @@ public class DateUtil extends CalendarUtil {
|
|
|
* @return 格式化后的字符串
|
|
|
*/
|
|
|
public static String format(LocalDateTime localDateTime, String format) {
|
|
|
- if (null == localDateTime || StrUtil.isBlank(format)) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
|
|
|
- return localDateTime.format(df);
|
|
|
+ return LocalDateTimeUtil.format(localDateTime, format);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -670,14 +661,7 @@ public class DateUtil extends CalendarUtil {
|
|
|
* @return LocalDateTime对象
|
|
|
*/
|
|
|
public static LocalDateTime parseLocalDateTime(CharSequence dateStr, String format) {
|
|
|
- dateStr = normalize(dateStr);
|
|
|
- DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
|
|
|
- try {
|
|
|
- return LocalDateTime.parse(dateStr, df);
|
|
|
- } catch (DateTimeParseException e) {
|
|
|
- // 在给定日期字符串没有时间部分时,LocalDateTime会报错,此时使用LocalDate中转转换
|
|
|
- return LocalDate.parse(dateStr, df).atStartOfDay();
|
|
|
- }
|
|
|
+ return LocalDateTimeUtil.parse(dateStr, format);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -835,7 +819,7 @@ public class DateUtil extends CalendarUtil {
|
|
|
} else if (length == DatePattern.UTC_MS_WITH_ZONE_OFFSET_PATTERN.length() + 2 || length == DatePattern.UTC_MS_WITH_ZONE_OFFSET_PATTERN.length() + 3) {
|
|
|
// 格式类似:2018-09-13T05:34:31.999+0800 或 2018-09-13T05:34:31.999+08:00
|
|
|
return parse(utcString, DatePattern.UTC_MS_WITH_ZONE_OFFSET_FORMAT);
|
|
|
- } else if(length == DatePattern.UTC_SIMPLE_PATTERN.length()-2){
|
|
|
+ } else if (length == DatePattern.UTC_SIMPLE_PATTERN.length() - 2) {
|
|
|
// 格式类似:2018-09-13T05:34:31
|
|
|
return parse(utcString, DatePattern.UTC_SIMPLE_FORMAT);
|
|
|
}
|
|
|
@@ -937,7 +921,7 @@ public class DateUtil extends CalendarUtil {
|
|
|
// yyyy-MM-dd HH:mm
|
|
|
return parse(dateStr, DatePattern.NORM_DATETIME_MINUTE_FORMAT);
|
|
|
case 2:
|
|
|
- if(StrUtil.contains(dateStr, CharUtil.DOT)){
|
|
|
+ if (StrUtil.contains(dateStr, CharUtil.DOT)) {
|
|
|
// yyyy-MM-dd HH:mm:ss.SSS
|
|
|
return parse(dateStr, DatePattern.NORM_DATETIME_MS_FORMAT);
|
|
|
}
|
|
|
@@ -1838,32 +1822,7 @@ public class DateUtil extends CalendarUtil {
|
|
|
* @since 5.0.2
|
|
|
*/
|
|
|
public static Instant toInstant(TemporalAccessor temporalAccessor) {
|
|
|
- if (null == temporalAccessor) {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- Instant result;
|
|
|
- if (temporalAccessor instanceof Instant) {
|
|
|
- result = (Instant) temporalAccessor;
|
|
|
- } else if (temporalAccessor instanceof LocalDateTime) {
|
|
|
- result = ((LocalDateTime) temporalAccessor).atZone(ZoneId.systemDefault()).toInstant();
|
|
|
- } else if (temporalAccessor instanceof ZonedDateTime) {
|
|
|
- result = ((ZonedDateTime) temporalAccessor).toInstant();
|
|
|
- } else if (temporalAccessor instanceof OffsetDateTime) {
|
|
|
- result = ((OffsetDateTime) temporalAccessor).toInstant();
|
|
|
- } else if (temporalAccessor instanceof LocalDate) {
|
|
|
- result = ((LocalDate) temporalAccessor).atStartOfDay(ZoneId.systemDefault()).toInstant();
|
|
|
- } else if (temporalAccessor instanceof LocalTime) {
|
|
|
- // 指定本地时间转换 为Instant,取当天日期
|
|
|
- result = ((LocalTime) temporalAccessor).atDate(LocalDate.now()).atZone(ZoneId.systemDefault()).toInstant();
|
|
|
- } else if (temporalAccessor instanceof OffsetTime) {
|
|
|
- // 指定本地时间转换 为Instant,取当天日期
|
|
|
- result = ((OffsetTime) temporalAccessor).atDate(LocalDate.now()).toInstant();
|
|
|
- } else {
|
|
|
- result = Instant.from(temporalAccessor);
|
|
|
- }
|
|
|
-
|
|
|
- return result;
|
|
|
+ return TemporalAccessorUtil.toInstant(temporalAccessor);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -1871,8 +1830,8 @@ public class DateUtil extends CalendarUtil {
|
|
|
*
|
|
|
* @param instant {@link Instant}
|
|
|
* @return {@link LocalDateTime}
|
|
|
- * @since 5.0.5
|
|
|
* @see LocalDateTimeUtil#of(Instant)
|
|
|
+ * @since 5.0.5
|
|
|
*/
|
|
|
public static LocalDateTime toLocalDateTime(Instant instant) {
|
|
|
return LocalDateTimeUtil.of(instant);
|
|
|
@@ -1883,8 +1842,8 @@ public class DateUtil extends CalendarUtil {
|
|
|
*
|
|
|
* @param date {@link Date}
|
|
|
* @return {@link LocalDateTime}
|
|
|
- * @since 5.0.5
|
|
|
* @see LocalDateTimeUtil#of(Date)
|
|
|
+ * @since 5.0.5
|
|
|
*/
|
|
|
public static LocalDateTime toLocalDateTime(Date date) {
|
|
|
return LocalDateTimeUtil.of(date);
|