mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
fix jdk8
This commit is contained in:
parent
5e8f36ce65
commit
ccff387646
@ -136,7 +136,7 @@ public class ConverterRegistry implements Serializable{
|
||||
* @return 转换器
|
||||
*/
|
||||
public <T> Converter<T> getConverter(Type type, boolean isCustomFirst) {
|
||||
Converter<T> converter = null;
|
||||
Converter<T> converter;
|
||||
if (isCustomFirst) {
|
||||
converter = this.getCustomConverter(type);
|
||||
if (null == converter) {
|
||||
|
@ -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包对象解析转换器<br>
|
||||
* 通过反射调用“parse方法”,支持的对象包括:
|
||||
@ -99,9 +102,12 @@ public class Jdk8DateConverter extends AbstractConverter<Object> {
|
||||
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<Object> {
|
||||
|
||||
/**
|
||||
* 通过反射将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<Object> getTargetType() {
|
||||
return (Class<Object>) this.targetType;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user