提交 c312b552 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Use LocalDateTime.toLocalTime() instead of obscured calculations in LocalDateTimeUtils

上级 39f12338
...@@ -85,8 +85,8 @@ public class LocalDateTimeUtils { ...@@ -85,8 +85,8 @@ public class LocalDateTimeUtils {
private static final Method LOCAL_DATE_TIME_PLUS_NANOS; private static final Method LOCAL_DATE_TIME_PLUS_NANOS;
// java.time.LocalDateTime#toLocalDate() // java.time.LocalDateTime#toLocalDate()
private static final Method LOCAL_DATE_TIME_TO_LOCAL_DATE; private static final Method LOCAL_DATE_TIME_TO_LOCAL_DATE;
// java.time.LocalDateTime#truncatedTo(TemporalUnit) // java.time.LocalDateTime#toLocalTime()
private static final Method LOCAL_DATE_TIME_TRUNCATED_TO; private static final Method LOCAL_DATE_TIME_TO_LOCAL_TIME;
// java.time.LocalDateTime#parse(CharSequence) // java.time.LocalDateTime#parse(CharSequence)
private static final Method LOCAL_DATE_TIME_PARSE; private static final Method LOCAL_DATE_TIME_PARSE;
...@@ -105,17 +105,8 @@ public class LocalDateTimeUtils { ...@@ -105,17 +105,8 @@ public class LocalDateTimeUtils {
// java.time.ZoneOffset#getTotalSeconds() // java.time.ZoneOffset#getTotalSeconds()
private static final Method ZONE_OFFSET_GET_TOTAL_SECONDS; private static final Method ZONE_OFFSET_GET_TOTAL_SECONDS;
// java.time.Duration#between(Temporal, Temporal)
private static final Method DURATION_BETWEEN;
// java.time.Duration#toNanos()
private static final Method DURATION_TO_NANOS;
// java.time.temporal.ChronoUnit#DAYS
private static final Object CHRONO_UNIT_DAYS;
private static final boolean IS_JAVA8_DATE_API_PRESENT; private static final boolean IS_JAVA8_DATE_API_PRESENT;
static { static {
LOCAL_DATE = tryGetClass("java.time.LocalDate"); LOCAL_DATE = tryGetClass("java.time.LocalDate");
LOCAL_TIME = tryGetClass("java.time.LocalTime"); LOCAL_TIME = tryGetClass("java.time.LocalTime");
...@@ -127,12 +118,6 @@ public class LocalDateTimeUtils { ...@@ -127,12 +118,6 @@ public class LocalDateTimeUtils {
ZONE_OFFSET != null; ZONE_OFFSET != null;
if (IS_JAVA8_DATE_API_PRESENT) { if (IS_JAVA8_DATE_API_PRESENT) {
Class<?> temporalUnit = getClass("java.time.temporal.TemporalUnit");
Class<?> chronoUnit = getClass("java.time.temporal.ChronoUnit");
Class<?> duration = getClass("java.time.Duration");
Class<?> temporal = getClass("java.time.temporal.Temporal");
LOCAL_TIME_OF_NANO = getMethod(LOCAL_TIME, "ofNanoOfDay", long.class); LOCAL_TIME_OF_NANO = getMethod(LOCAL_TIME, "ofNanoOfDay", long.class);
LOCAL_TIME_TO_NANO = getMethod(LOCAL_TIME, "toNanoOfDay"); LOCAL_TIME_TO_NANO = getMethod(LOCAL_TIME, "toNanoOfDay");
...@@ -150,7 +135,7 @@ public class LocalDateTimeUtils { ...@@ -150,7 +135,7 @@ public class LocalDateTimeUtils {
LOCAL_DATE_TIME_PLUS_NANOS = getMethod(LOCAL_DATE_TIME, "plusNanos", long.class); LOCAL_DATE_TIME_PLUS_NANOS = getMethod(LOCAL_DATE_TIME, "plusNanos", long.class);
LOCAL_DATE_TIME_TO_LOCAL_DATE = getMethod(LOCAL_DATE_TIME, "toLocalDate"); LOCAL_DATE_TIME_TO_LOCAL_DATE = getMethod(LOCAL_DATE_TIME, "toLocalDate");
LOCAL_DATE_TIME_TRUNCATED_TO = getMethod(LOCAL_DATE_TIME, "truncatedTo", temporalUnit); LOCAL_DATE_TIME_TO_LOCAL_TIME = getMethod(LOCAL_DATE_TIME, "toLocalTime");
LOCAL_DATE_TIME_PARSE = getMethod(LOCAL_DATE_TIME, "parse", CharSequence.class); LOCAL_DATE_TIME_PARSE = getMethod(LOCAL_DATE_TIME, "parse", CharSequence.class);
ZONE_OFFSET_OF_TOTAL_SECONDS = getMethod(ZONE_OFFSET, "ofTotalSeconds", int.class); ZONE_OFFSET_OF_TOTAL_SECONDS = getMethod(ZONE_OFFSET, "ofTotalSeconds", int.class);
...@@ -162,11 +147,6 @@ public class LocalDateTimeUtils { ...@@ -162,11 +147,6 @@ public class LocalDateTimeUtils {
OFFSET_DATE_TIME_PARSE = getMethod(OFFSET_DATE_TIME, "parse", CharSequence.class); OFFSET_DATE_TIME_PARSE = getMethod(OFFSET_DATE_TIME, "parse", CharSequence.class);
ZONE_OFFSET_GET_TOTAL_SECONDS = getMethod(ZONE_OFFSET, "getTotalSeconds"); ZONE_OFFSET_GET_TOTAL_SECONDS = getMethod(ZONE_OFFSET, "getTotalSeconds");
DURATION_BETWEEN = getMethod(duration, "between", temporal, temporal);
DURATION_TO_NANOS = getMethod(duration, "toNanos");
CHRONO_UNIT_DAYS = getFieldValue(chronoUnit, "DAYS");
} else { } else {
LOCAL_TIME_OF_NANO = null; LOCAL_TIME_OF_NANO = null;
LOCAL_TIME_TO_NANO = null; LOCAL_TIME_TO_NANO = null;
...@@ -179,7 +159,7 @@ public class LocalDateTimeUtils { ...@@ -179,7 +159,7 @@ public class LocalDateTimeUtils {
LOCAL_TIME_PARSE = null; LOCAL_TIME_PARSE = null;
LOCAL_DATE_TIME_PLUS_NANOS = null; LOCAL_DATE_TIME_PLUS_NANOS = null;
LOCAL_DATE_TIME_TO_LOCAL_DATE = null; LOCAL_DATE_TIME_TO_LOCAL_DATE = null;
LOCAL_DATE_TIME_TRUNCATED_TO = null; LOCAL_DATE_TIME_TO_LOCAL_TIME = null;
LOCAL_DATE_TIME_PARSE = null; LOCAL_DATE_TIME_PARSE = null;
ZONE_OFFSET_OF_TOTAL_SECONDS = null; ZONE_OFFSET_OF_TOTAL_SECONDS = null;
OFFSET_DATE_TIME_TO_LOCAL_DATE_TIME = null; OFFSET_DATE_TIME_TO_LOCAL_DATE_TIME = null;
...@@ -187,9 +167,6 @@ public class LocalDateTimeUtils { ...@@ -187,9 +167,6 @@ public class LocalDateTimeUtils {
OFFSET_DATE_TIME_OF_LOCAL_DATE_TIME_ZONE_OFFSET = null; OFFSET_DATE_TIME_OF_LOCAL_DATE_TIME_ZONE_OFFSET = null;
OFFSET_DATE_TIME_PARSE = null; OFFSET_DATE_TIME_PARSE = null;
ZONE_OFFSET_GET_TOTAL_SECONDS = null; ZONE_OFFSET_GET_TOTAL_SECONDS = null;
DURATION_BETWEEN = null;
DURATION_TO_NANOS = null;
CHRONO_UNIT_DAYS = null;
} }
} }
...@@ -310,15 +287,6 @@ public class LocalDateTimeUtils { ...@@ -310,15 +287,6 @@ public class LocalDateTimeUtils {
} }
} }
private static Class<?> getClass(String className) {
try {
return Class.forName(className);
} catch (ClassNotFoundException e) {
throw new IllegalStateException("Java 8 or later but class " +
className + " is missing", e);
}
}
private static Method getMethod(Class<?> clazz, String methodName, private static Method getMethod(Class<?> clazz, String methodName,
Class<?>... parameterTypes) { Class<?>... parameterTypes) {
try { try {
...@@ -330,15 +298,6 @@ public class LocalDateTimeUtils { ...@@ -330,15 +298,6 @@ public class LocalDateTimeUtils {
} }
} }
private static Object getFieldValue(Class<?> clazz, String fieldName) {
try {
return clazz.getField(fieldName).get(null);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new IllegalStateException("Java 8 or later but field " +
clazz.getName() + "#" + fieldName + " is missing", e);
}
}
/** /**
* Checks if the given class is LocalDate. * Checks if the given class is LocalDate.
* *
...@@ -566,9 +525,8 @@ public class LocalDateTimeUtils { ...@@ -566,9 +525,8 @@ public class LocalDateTimeUtils {
private static long timeNanosFromLocalDateTime(Object localDateTime) private static long timeNanosFromLocalDateTime(Object localDateTime)
throws IllegalAccessException, InvocationTargetException { throws IllegalAccessException, InvocationTargetException {
Object midnight = LOCAL_DATE_TIME_TRUNCATED_TO.invoke(localDateTime, CHRONO_UNIT_DAYS); Object localTime = LOCAL_DATE_TIME_TO_LOCAL_TIME.invoke(localDateTime);
Object duration = DURATION_BETWEEN.invoke(null, midnight, localDateTime); return (Long) LOCAL_TIME_TO_NANO.invoke(localTime);
return (Long) DURATION_TO_NANOS.invoke(duration);
} }
private static short zoneOffsetToOffsetMinute(Object zoneOffset) private static short zoneOffsetToOffsetMinute(Object zoneOffset)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论