org.springframework.format.datetime.standard.DateTimeConverters Maven / Gradle / Ivy
Show all versions of spring-context Show documentation
/*
* Copyright 2002-2014 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
*
* http://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.format.datetime.standard;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.time.chrono.ChronoZonedDateTime;
import java.util.Calendar;
import java.util.GregorianCalendar;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterRegistry;
import org.springframework.format.datetime.DateFormatterRegistrar;
import org.springframework.lang.UsesJava8;
/**
* Installs lower-level type converters required to integrate
* JSR-310 support into Spring's field formatting system.
*
* Note: {@link DateTimeFormatterRegistrar} installs these converters but
* does not rely on them for its formatters. They are just being registered
* for custom conversion scenarios between different JSR-310 value types
* and also between {@link java.util.Calendar} and JSR-310 value types.
*
* @author Juergen Hoeller
* @since 4.0.1
*/
@UsesJava8
final class DateTimeConverters {
/**
* Install the converters into the converter registry.
* @param registry the converter registry
*/
public static void registerConverters(ConverterRegistry registry) {
DateFormatterRegistrar.addDateConverters(registry);
registry.addConverter(new LocalDateTimeToLocalDateConverter());
registry.addConverter(new LocalDateTimeToLocalTimeConverter());
registry.addConverter(new ZonedDateTimeToLocalDateConverter());
registry.addConverter(new ZonedDateTimeToLocalTimeConverter());
registry.addConverter(new ZonedDateTimeToLocalDateTimeConverter());
registry.addConverter(new ZonedDateTimeToOffsetDateTimeConverter());
registry.addConverter(new ZonedDateTimeToInstantConverter());
registry.addConverter(new OffsetDateTimeToLocalDateConverter());
registry.addConverter(new OffsetDateTimeToLocalTimeConverter());
registry.addConverter(new OffsetDateTimeToLocalDateTimeConverter());
registry.addConverter(new OffsetDateTimeToZonedDateTimeConverter());
registry.addConverter(new OffsetDateTimeToInstantConverter());
registry.addConverter(new CalendarToZonedDateTimeConverter());
registry.addConverter(new CalendarToOffsetDateTimeConverter());
registry.addConverter(new CalendarToLocalDateConverter());
registry.addConverter(new CalendarToLocalTimeConverter());
registry.addConverter(new CalendarToLocalDateTimeConverter());
registry.addConverter(new CalendarToInstantConverter());
registry.addConverter(new LongToInstantConverter());
registry.addConverter(new InstantToLongConverter());
}
private static ZonedDateTime calendarToZonedDateTime(Calendar source) {
if (source instanceof GregorianCalendar) {
return ((GregorianCalendar) source).toZonedDateTime();
}
else {
return ZonedDateTime.ofInstant(Instant.ofEpochMilli(source.getTimeInMillis()),
source.getTimeZone().toZoneId());
}
}
@UsesJava8
private static class LocalDateTimeToLocalDateConverter implements Converter {
@Override
public LocalDate convert(LocalDateTime source) {
return source.toLocalDate();
}
}
@UsesJava8
private static class LocalDateTimeToLocalTimeConverter implements Converter {
@Override
public LocalTime convert(LocalDateTime source) {
return source.toLocalTime();
}
}
@UsesJava8
private static class ZonedDateTimeToLocalDateConverter implements Converter {
@Override
public LocalDate convert(ZonedDateTime source) {
return source.toLocalDate();
}
}
@UsesJava8
private static class ZonedDateTimeToLocalTimeConverter implements Converter {
@Override
public LocalTime convert(ZonedDateTime source) {
return source.toLocalTime();
}
}
@UsesJava8
private static class ZonedDateTimeToLocalDateTimeConverter implements Converter {
@Override
public LocalDateTime convert(ZonedDateTime source) {
return source.toLocalDateTime();
}
}
@UsesJava8
private static class ZonedDateTimeToOffsetDateTimeConverter implements Converter {
@Override
public OffsetDateTime convert(ZonedDateTime source) {
return source.toOffsetDateTime();
}
}
@UsesJava8
private static class ZonedDateTimeToInstantConverter implements Converter {
@Override
public Instant convert(ZonedDateTime source) {
// Explicit cast to interface necessary in order to call Java 8 default method from -source 1.6
return ((ChronoZonedDateTime) source).toInstant();
}
}
@UsesJava8
private static class OffsetDateTimeToLocalDateConverter implements Converter {
@Override
public LocalDate convert(OffsetDateTime source) {
return source.toLocalDate();
}
}
@UsesJava8
private static class OffsetDateTimeToLocalTimeConverter implements Converter {
@Override
public LocalTime convert(OffsetDateTime source) {
return source.toLocalTime();
}
}
@UsesJava8
private static class OffsetDateTimeToLocalDateTimeConverter implements Converter {
@Override
public LocalDateTime convert(OffsetDateTime source) {
return source.toLocalDateTime();
}
}
@UsesJava8
private static class OffsetDateTimeToZonedDateTimeConverter implements Converter {
@Override
public ZonedDateTime convert(OffsetDateTime source) {
return source.toZonedDateTime();
}
}
@UsesJava8
private static class OffsetDateTimeToInstantConverter implements Converter {
@Override
public Instant convert(OffsetDateTime source) {
return source.toInstant();
}
}
@UsesJava8
private static class CalendarToZonedDateTimeConverter implements Converter {
@Override
public ZonedDateTime convert(Calendar source) {
return calendarToZonedDateTime(source);
}
}
@UsesJava8
private static class CalendarToOffsetDateTimeConverter implements Converter {
@Override
public OffsetDateTime convert(Calendar source) {
return calendarToZonedDateTime(source).toOffsetDateTime();
}
}
@UsesJava8
private static class CalendarToLocalDateConverter implements Converter {
@Override
public LocalDate convert(Calendar source) {
return calendarToZonedDateTime(source).toLocalDate();
}
}
@UsesJava8
private static class CalendarToLocalTimeConverter implements Converter {
@Override
public LocalTime convert(Calendar source) {
return calendarToZonedDateTime(source).toLocalTime();
}
}
@UsesJava8
private static class CalendarToLocalDateTimeConverter implements Converter {
@Override
public LocalDateTime convert(Calendar source) {
return calendarToZonedDateTime(source).toLocalDateTime();
}
}
@UsesJava8
private static class CalendarToInstantConverter implements Converter {
@Override
public Instant convert(Calendar source) {
// Explicit cast to interface necessary in order to call Java 8 default method from -source 1.6
return ((ChronoZonedDateTime) calendarToZonedDateTime(source)).toInstant();
}
}
@UsesJava8
private static class LongToInstantConverter implements Converter {
@Override
public Instant convert(Long source) {
return Instant.ofEpochMilli(source);
}
}
@UsesJava8
private static class InstantToLongConverter implements Converter {
@Override
public Long convert(Instant source) {
return source.toEpochMilli();
}
}
}