public class LocalDateTimeUtils extends Object
This class is implemented using reflection so that it compiles on Java 7 as well.
For LocalDate and LocalTime the conversion methods provided by the JDK are used. For OffsetDateTime a custom conversion method is used because it has no equivalent in JDBC. For LocalDateTime a custom conversion method is used instead of the one provided by the JDK as well.
Using the JDK provided conversion method for LocalDateTime would introduces some errors in edge cases. Consider the following case: at 2016-03-27 02:00 in Europe/Berlin the clocks were set to 2016-03-27 03:00. This means that 2016-03-27 02:15 does not exist in Europe/Berlin. Unfortunately java.sql.Timestamp is in the the time zone of the JVM. That means if you run a JVM with the time zone Europe/Berlin then the SQL value 'TIMESTAMP 2016-03-27 02:15:00' can not be represented. java.time.LocalDateTime does not have these limitations but if we convert through java.sql.Timestamp we inherit its limitations. Therefore that conversion must be avoided.
Once the driver requires Java 8 all the reflection can be removed.
Modifier and Type | Method and Description |
---|---|
static Class<?> |
getLocalDateClass()
Returns the class java.time.LocalDate.
|
static Class<?> |
getLocalDateTimeClass()
Returns the class java.time.LocalDateTime.
|
static Class<?> |
getLocalTimeClass()
Returns the class java.time.LocalTime.
|
static Class<?> |
getOffsetDateTimeClass()
Returns the class java.time.OffsetDateTime.
|
static boolean |
isJava8DateApiPresent()
Checks if the Java 8 Date and Time API is present.
|
static boolean |
isLocalDate(Class<?> clazz)
Checks if the given class is LocalDate.
|
static boolean |
isLocalDateTime(Class<?> clazz)
Checks if the given class is LocalDateTime.
|
static boolean |
isLocalTime(Class<?> clazz)
Checks if the given class is LocalTime.
|
static boolean |
isOffsetDateTime(Class<?> clazz)
Checks if the given class is OffsetDateTime.
|
static Value |
localDateTimeToValue(Object localDateTime)
Converts a LocalDateTime to a Value.
|
static Value |
localDateToDateValue(Object localDate)
Converts a LocalDate to a Value.
|
static Value |
localTimeToTimeValue(Object localTime)
Converts a LocalTime to a Value.
|
static Value |
offsetDateTimeToValue(Object offsetDateTime)
Converts a OffsetDateTime to a Value.
|
static Object |
parseLocalDate(CharSequence text)
Parses an ISO date string into a java.time.LocalDate.
|
static Object |
parseLocalDateTime(CharSequence text)
Parses an ISO date string into a java.time.LocalDateTime.
|
static Object |
parseLocalTime(CharSequence text)
Parses an ISO time string into a java.time.LocalTime.
|
static Object |
parseOffsetDateTime(CharSequence text)
Parses an ISO date string into a java.time.OffsetDateTime.
|
static Object |
valueToLocalDate(Value value)
Converts a value to a LocalDate.
|
static Object |
valueToLocalDateTime(ValueTimestamp value)
Converts a value to a LocalDateTime.
|
static Object |
valueToLocalTime(Value value)
Converts a value to a LocalTime.
|
static Object |
valueToOffsetDateTime(ValueTimestampTimeZone value)
Converts a value to a OffsetDateTime.
|
public static boolean isJava8DateApiPresent()
This is the case on Java 8 and later and not the case on Java 7. Versions older than Java 7 are not supported.
public static Class<?> getLocalDateClass()
public static Class<?> getLocalTimeClass()
public static Class<?> getLocalDateTimeClass()
public static Class<?> getOffsetDateTimeClass()
public static Object parseLocalDate(CharSequence text)
text
- the ISO date stringpublic static Object parseLocalTime(CharSequence text)
text
- the ISO time stringpublic static Object parseLocalDateTime(CharSequence text)
text
- the ISO date stringpublic static Object parseOffsetDateTime(CharSequence text)
text
- the ISO date stringpublic static boolean isLocalDate(Class<?> clazz)
This method can be called from Java 7.
clazz
- the class to checkpublic static boolean isLocalTime(Class<?> clazz)
This method can be called from Java 7.
clazz
- the class to checkpublic static boolean isLocalDateTime(Class<?> clazz)
This method can be called from Java 7.
clazz
- the class to checkpublic static boolean isOffsetDateTime(Class<?> clazz)
This method can be called from Java 7.
clazz
- the class to checkpublic static Object valueToLocalDate(Value value)
This method should only called from Java 8 or later.
value
- the value to convertpublic static Object valueToLocalTime(Value value)
This method should only called from Java 8 or later.
value
- the value to convertpublic static Object valueToLocalDateTime(ValueTimestamp value)
This method should only called from Java 8 or later.
value
- the value to convertpublic static Object valueToOffsetDateTime(ValueTimestampTimeZone value)
This method should only called from Java 8 or later.
value
- the value to convertpublic static Value localDateToDateValue(Object localDate)
localDate
- the LocalDate to convert, not null
public static Value localTimeToTimeValue(Object localTime)
localTime
- the LocalTime to convert, not null
public static Value localDateTimeToValue(Object localDateTime)
localDateTime
- the LocalDateTime to convert, not null
Copyright © 2017 JBoss by Red Hat. All rights reserved.