Browse Source
The standard JSR 310 converters are no longer used for conversions between java.util.Date and java.time.*. New converters based converting to/from Timestamp are used. This preserves the precision because both the java.time.* API and Timestamp have nanosecond precision, while java.util.Date has not. Original pull request: #254.pull/1035/head
8 changed files with 274 additions and 37 deletions
@ -0,0 +1,174 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2020 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.springframework.data.jdbc.core.convert; |
||||||
|
|
||||||
|
import static java.time.Instant.*; |
||||||
|
import static java.time.LocalDateTime.*; |
||||||
|
import static java.time.ZoneId.*; |
||||||
|
|
||||||
|
import java.sql.Timestamp; |
||||||
|
import java.time.Duration; |
||||||
|
import java.time.Instant; |
||||||
|
import java.time.LocalDate; |
||||||
|
import java.time.LocalDateTime; |
||||||
|
import java.time.LocalTime; |
||||||
|
import java.time.Period; |
||||||
|
import java.time.ZoneId; |
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.Arrays; |
||||||
|
import java.util.Collection; |
||||||
|
import java.util.Date; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
import org.springframework.core.convert.converter.Converter; |
||||||
|
import org.springframework.data.convert.ReadingConverter; |
||||||
|
import org.springframework.data.convert.WritingConverter; |
||||||
|
import org.springframework.lang.NonNull; |
||||||
|
|
||||||
|
/** |
||||||
|
* Helper class to register JSR-310 specific {@link Converter} implementations. These converters are based on |
||||||
|
* {@link java.sql.Timestamp} instead of {@link Date} and therefore preserve nanosecond precision |
||||||
|
* |
||||||
|
* @see org.springframework.data.convert.Jsr310Converters |
||||||
|
* @author Jens Schauder |
||||||
|
* @since 2.2 |
||||||
|
*/ |
||||||
|
public abstract class Jsr310TimestampBasedConverters { |
||||||
|
|
||||||
|
private static final List<Class<?>> CLASSES = Arrays.asList(LocalDateTime.class, LocalDate.class, LocalTime.class, |
||||||
|
Instant.class, ZoneId.class, Duration.class, Period.class); |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the converters to be registered. Will only return converters in case we're running on Java 8. |
||||||
|
* |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
public static Collection<Converter<?, ?>> getConvertersToRegister() { |
||||||
|
|
||||||
|
List<Converter<?, ?>> converters = new ArrayList<>(); |
||||||
|
converters.add(TimestampToLocalDateTimeConverter.INSTANCE); |
||||||
|
converters.add(LocalDateTimeToTimestampConverter.INSTANCE); |
||||||
|
converters.add(TimestampToLocalDateConverter.INSTANCE); |
||||||
|
converters.add(LocalDateToTimestampConverter.INSTANCE); |
||||||
|
converters.add(TimestampToLocalTimeConverter.INSTANCE); |
||||||
|
converters.add(LocalTimeToTimestampConverter.INSTANCE); |
||||||
|
converters.add(TimestampToInstantConverter.INSTANCE); |
||||||
|
converters.add(InstantToTimestampConverter.INSTANCE); |
||||||
|
|
||||||
|
return converters; |
||||||
|
} |
||||||
|
|
||||||
|
public static boolean supports(Class<?> type) { |
||||||
|
|
||||||
|
return CLASSES.contains(type); |
||||||
|
} |
||||||
|
|
||||||
|
@ReadingConverter |
||||||
|
public enum TimestampToLocalDateTimeConverter implements Converter<Timestamp, LocalDateTime> { |
||||||
|
|
||||||
|
INSTANCE; |
||||||
|
|
||||||
|
@NonNull |
||||||
|
@Override |
||||||
|
public LocalDateTime convert(Timestamp source) { |
||||||
|
return ofInstant(source.toInstant(), systemDefault()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@WritingConverter |
||||||
|
public enum LocalDateTimeToTimestampConverter implements Converter<LocalDateTime, Timestamp> { |
||||||
|
|
||||||
|
INSTANCE; |
||||||
|
|
||||||
|
@NonNull |
||||||
|
@Override |
||||||
|
public Timestamp convert(LocalDateTime source) { |
||||||
|
return Timestamp.from(source.atZone(systemDefault()).toInstant()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@ReadingConverter |
||||||
|
public enum TimestampToLocalDateConverter implements Converter<Timestamp, LocalDate> { |
||||||
|
|
||||||
|
INSTANCE; |
||||||
|
|
||||||
|
@NonNull |
||||||
|
@Override |
||||||
|
public LocalDate convert(Timestamp source) { |
||||||
|
return source.toLocalDateTime().toLocalDate(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@WritingConverter |
||||||
|
public enum LocalDateToTimestampConverter implements Converter<LocalDate, Timestamp> { |
||||||
|
|
||||||
|
INSTANCE; |
||||||
|
|
||||||
|
@NonNull |
||||||
|
@Override |
||||||
|
public Timestamp convert(LocalDate source) { |
||||||
|
return Timestamp.from(source.atStartOfDay(systemDefault()).toInstant()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@ReadingConverter |
||||||
|
public enum TimestampToLocalTimeConverter implements Converter<Timestamp, LocalTime> { |
||||||
|
|
||||||
|
INSTANCE; |
||||||
|
|
||||||
|
@NonNull |
||||||
|
@Override |
||||||
|
public LocalTime convert(Timestamp source) { |
||||||
|
return source.toLocalDateTime().toLocalTime(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@WritingConverter |
||||||
|
public enum LocalTimeToTimestampConverter implements Converter<LocalTime, Timestamp> { |
||||||
|
|
||||||
|
INSTANCE; |
||||||
|
|
||||||
|
@NonNull |
||||||
|
@Override |
||||||
|
public Timestamp convert(LocalTime source) { |
||||||
|
return Timestamp.from(source.atDate(LocalDate.now()).atZone(systemDefault()).toInstant()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@ReadingConverter |
||||||
|
public enum TimestampToInstantConverter implements Converter<Timestamp, Instant> { |
||||||
|
|
||||||
|
INSTANCE; |
||||||
|
|
||||||
|
@NonNull |
||||||
|
@Override |
||||||
|
public Instant convert(Timestamp source) { |
||||||
|
return source.toInstant(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@WritingConverter |
||||||
|
public enum InstantToTimestampConverter implements Converter<Instant, Timestamp> { |
||||||
|
|
||||||
|
INSTANCE; |
||||||
|
|
||||||
|
@NonNull |
||||||
|
@Override |
||||||
|
public Timestamp convert(Instant source) { |
||||||
|
return Timestamp.from(source); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue