diff --git a/hutool-core/src/main/java/cn/hutool/core/convert/ConverterRegistry.java b/hutool-core/src/main/java/cn/hutool/core/convert/ConverterRegistry.java index 08d95e1a4..49632c3fc 100644 --- a/hutool-core/src/main/java/cn/hutool/core/convert/ConverterRegistry.java +++ b/hutool-core/src/main/java/cn/hutool/core/convert/ConverterRegistry.java @@ -136,7 +136,7 @@ public class ConverterRegistry implements Serializable{ * @return 转换器 */ public Converter getConverter(Type type, boolean isCustomFirst) { - Converter converter = null; + Converter converter; if (isCustomFirst) { converter = this.getCustomConverter(type); if (null == converter) { diff --git a/hutool-core/src/main/java/cn/hutool/core/convert/impl/Jdk8DateConverter.java b/hutool-core/src/main/java/cn/hutool/core/convert/impl/Jdk8DateConverter.java index 528303306..e3eede993 100644 --- a/hutool-core/src/main/java/cn/hutool/core/convert/impl/Jdk8DateConverter.java +++ b/hutool-core/src/main/java/cn/hutool/core/convert/impl/Jdk8DateConverter.java @@ -1,11 +1,14 @@ package cn.hutool.core.convert.impl; -import java.lang.reflect.Method; - import cn.hutool.core.convert.AbstractConverter; -import cn.hutool.core.util.ClassUtil; import cn.hutool.core.util.ReflectUtil; +import java.lang.reflect.Method; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + /** * JDK8中新加入的java.time包对象解析转换器
* 通过反射调用“parse方法”,支持的对象包括: @@ -99,9 +102,12 @@ public class Jdk8DateConverter extends AbstractConverter { private Object parseFromCharSequence(CharSequence value) { Method method; if (null != this.format) { - final Object dateTimeFormatter = getDateTimeFormatter(); - method = ReflectUtil.getMethod(this.targetType, "parse", CharSequence.class, dateTimeFormatter.getClass()); - return ReflectUtil.invokeStatic(method, value, dateTimeFormatter); + final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(this.format); + method = ReflectUtil.getMethod(this.targetType, "parse", CharSequence.class, DateTimeFormatter.class); + if(Instant.class.isAssignableFrom(this.targetType)){ + return formatter.parse(value, Instant::from); + } + return ReflectUtil.invokeStatic(method, value, formatter); } else { method = ReflectUtil.getMethod(this.targetType, "parse", CharSequence.class); return ReflectUtil.invokeStatic(method, value); @@ -110,43 +116,15 @@ public class Jdk8DateConverter extends AbstractConverter { /** * 通过反射将Long型时间戳转换为java.time中的对象 - * + * * @param time 时间戳 * @return java.time中的对象 */ private Object parseFromLong(Long time) { String targetName = this.targetType.getName(); if ("java.time.Instant".equals(targetName)) { - return toInstant(time); + return Instant.ofEpochMilli(time); } return null; } - - /** - * 反射获取java.time.format.DateTimeFormatter对象 - * - * @return java.time.format.DateTimeFormatter对象 - */ - private Object getDateTimeFormatter() { - if (null != this.format) { - return ClassUtil.invoke("java.time.format.DateTimeFormatter.ofPattern", false, this.format); - } - return null; - } - - /** - * Long转 java.time.Instant - * - * @param time 时间戳 - * @return java.time.Instant - */ - private Object toInstant(Long time) { - return ClassUtil.invoke("java.time.Instant.ofEpochMilli", false, time); - } - - @SuppressWarnings("unchecked") - @Override - public Class getTargetType() { - return (Class) this.targetType; - } }