All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.format.datetime.joda.JodaTimeConverters Maven / Gradle / Ivy

/*
 * 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.joda;

import java.util.Calendar;
import java.util.Date;

import org.joda.time.DateTime;
import org.joda.time.Instant;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.joda.time.LocalTime;
import org.joda.time.MutableDateTime;
import org.joda.time.ReadableInstant;

import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterRegistry;
import org.springframework.format.datetime.DateFormatterRegistrar;

/**
 * Installs lower-level type converters required to integrate
 * Joda-Time support into Spring's field formatting system.
 *
 * 

Note: {@link JodaTimeFormatterRegistrar} installs these converters * and relies on several of them for its formatters. Some additional * converters are just being registered for custom conversion scenarios. * * @author Keith Donald * @author Phillip Webb * @author Juergen Hoeller * @since 3.0 */ final class JodaTimeConverters { /** * Install the converters into the converter registry. * @param registry the converter registry */ @SuppressWarnings("deprecation") public static void registerConverters(ConverterRegistry registry) { DateFormatterRegistrar.addDateConverters(registry); registry.addConverter(new DateTimeToLocalDateConverter()); registry.addConverter(new DateTimeToLocalTimeConverter()); registry.addConverter(new DateTimeToLocalDateTimeConverter()); registry.addConverter(new DateTimeToDateMidnightConverter()); registry.addConverter(new DateTimeToMutableDateTimeConverter()); registry.addConverter(new DateTimeToInstantConverter()); registry.addConverter(new DateTimeToDateConverter()); registry.addConverter(new DateTimeToCalendarConverter()); registry.addConverter(new DateTimeToLongConverter()); registry.addConverter(new DateToReadableInstantConverter()); registry.addConverter(new CalendarToReadableInstantConverter()); registry.addConverter(new LongToReadableInstantConverter()); registry.addConverter(new LocalDateTimeToLocalDateConverter()); registry.addConverter(new LocalDateTimeToLocalTimeConverter()); } private static class DateTimeToLocalDateConverter implements Converter { @Override public LocalDate convert(DateTime source) { return source.toLocalDate(); } } private static class DateTimeToLocalTimeConverter implements Converter { @Override public LocalTime convert(DateTime source) { return source.toLocalTime(); } } private static class DateTimeToLocalDateTimeConverter implements Converter { @Override public LocalDateTime convert(DateTime source) { return source.toLocalDateTime(); } } @Deprecated private static class DateTimeToDateMidnightConverter implements Converter { @Override public org.joda.time.DateMidnight convert(DateTime source) { return source.toDateMidnight(); } } private static class DateTimeToMutableDateTimeConverter implements Converter { @Override public MutableDateTime convert(DateTime source) { return source.toMutableDateTime(); } } private static class DateTimeToInstantConverter implements Converter { @Override public Instant convert(DateTime source) { return source.toInstant(); } } private static class DateTimeToDateConverter implements Converter { @Override public Date convert(DateTime source) { return source.toDate(); } } private static class DateTimeToCalendarConverter implements Converter { @Override public Calendar convert(DateTime source) { return source.toGregorianCalendar(); } } private static class DateTimeToLongConverter implements Converter { @Override public Long convert(DateTime source) { return source.getMillis(); } } /** * Used when printing a {@code java.util.Date} field with a ReadableInstantPrinter. * @see MillisecondInstantPrinter * @see JodaDateTimeFormatAnnotationFormatterFactory */ private static class DateToReadableInstantConverter implements Converter { @Override public ReadableInstant convert(Date source) { return new DateTime(source); } } /** * Used when printing a {@code java.util.Calendar} field with a ReadableInstantPrinter. * @see MillisecondInstantPrinter * @see JodaDateTimeFormatAnnotationFormatterFactory */ private static class CalendarToReadableInstantConverter implements Converter { @Override public ReadableInstant convert(Calendar source) { return new DateTime(source); } } /** * Used when printing a Long field with a ReadableInstantPrinter. * @see MillisecondInstantPrinter * @see JodaDateTimeFormatAnnotationFormatterFactory */ private static class LongToReadableInstantConverter implements Converter { @Override public ReadableInstant convert(Long source) { return new DateTime(source.longValue()); } } private static class LocalDateTimeToLocalDateConverter implements Converter { @Override public LocalDate convert(LocalDateTime source) { return source.toLocalDate(); } } private static class LocalDateTimeToLocalTimeConverter implements Converter { @Override public LocalTime convert(LocalDateTime source) { return source.toLocalTime(); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy