|
| 1 | +/* |
| 2 | + * Copyright 2002-2009 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.core.convert.support; |
| 17 | + |
| 18 | +import java.util.Calendar; |
| 19 | +import java.util.Date; |
| 20 | + |
| 21 | +import org.joda.time.DateMidnight; |
| 22 | +import org.joda.time.DateTime; |
| 23 | +import org.joda.time.LocalDate; |
| 24 | +import org.joda.time.LocalDateTime; |
| 25 | +import org.joda.time.LocalTime; |
| 26 | +import org.springframework.core.convert.converter.Converter; |
| 27 | +import org.springframework.util.ClassUtils; |
| 28 | + |
| 29 | +class JodaTimeConverters { |
| 30 | + |
| 31 | + public static void addConverters(GenericConversionService registry) { |
| 32 | + if (!isJodaTimePresent()) { |
| 33 | + return; |
| 34 | + } |
| 35 | + registry.addConverter(DateTime.class, LocalDate.class, new DateTimeToLocalDateConverter()); |
| 36 | + registry.addConverter(LocalDate.class, DateTime.class, new LocalDateToDateTimeConverter()); |
| 37 | + |
| 38 | + registry.addConverter(DateTime.class, LocalTime.class, new DateTimeToLocalTimeConverter()); |
| 39 | + registry.addConverter(LocalTime.class, DateTime.class, new LocalTimeToDateTimeConverter()); |
| 40 | + |
| 41 | + registry.addConverter(DateTime.class, LocalDateTime.class, new DateTimeToLocalDateTimeConverter()); |
| 42 | + registry.addConverter(LocalDateTime.class, DateTime.class, new LocalDateTimeToDateTimeConverter()); |
| 43 | + |
| 44 | + registry.addConverter(DateTime.class, DateMidnight.class, new DateTimeToDateMidnightConverter()); |
| 45 | + registry.addConverter(DateMidnight.class, Date.class, new DateMidnightToDateTimeConverter()); |
| 46 | + |
| 47 | + registry.addConverter(DateTime.class, Date.class, new DateTimeToDateConverter()); |
| 48 | + registry.addConverter(Date.class, DateTime.class, new DateToDateTimeConverter()); |
| 49 | + |
| 50 | + registry.addConverter(DateTime.class, Calendar.class, new DateTimeToCalendarConverter()); |
| 51 | + registry.addConverter(Calendar.class, DateTime.class, new CalendarToDateTimeConverter()); |
| 52 | + } |
| 53 | + |
| 54 | + private static boolean isJodaTimePresent() { |
| 55 | + return ClassUtils.isPresent("org.joda.time.DateTime", JodaTimeConverters.class.getClassLoader()); |
| 56 | + } |
| 57 | + |
| 58 | + private static class DateTimeToLocalDateConverter implements Converter<DateTime, LocalDate> { |
| 59 | + public LocalDate convert(DateTime source) { |
| 60 | + return source.toLocalDate(); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private static class LocalDateToDateTimeConverter implements Converter<LocalDate, DateTime> { |
| 65 | + public DateTime convert(LocalDate source) { |
| 66 | + return source.toDateTimeAtStartOfDay(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private static class DateTimeToLocalTimeConverter implements Converter<DateTime, LocalTime> { |
| 71 | + public LocalTime convert(DateTime source) { |
| 72 | + return source.toLocalTime(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private static class LocalTimeToDateTimeConverter implements Converter<LocalTime, DateTime> { |
| 77 | + public DateTime convert(LocalTime source) { |
| 78 | + return source.toDateTimeToday(); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private static class DateTimeToLocalDateTimeConverter implements Converter<DateTime, LocalDateTime> { |
| 83 | + public LocalDateTime convert(DateTime source) { |
| 84 | + return source.toLocalDateTime(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private static class LocalDateTimeToDateTimeConverter implements Converter<LocalDateTime, DateTime> { |
| 89 | + public DateTime convert(LocalDateTime source) { |
| 90 | + return source.toDateTime(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private static class DateTimeToDateMidnightConverter implements Converter<DateTime, DateMidnight> { |
| 95 | + public DateMidnight convert(DateTime source) { |
| 96 | + return source.toDateMidnight(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private static class DateMidnightToDateTimeConverter implements Converter<DateMidnight, DateTime> { |
| 101 | + public DateTime convert(DateMidnight source) { |
| 102 | + return source.toDateTime(); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private static class DateTimeToDateConverter implements Converter<DateTime, Date> { |
| 107 | + public Date convert(DateTime source) { |
| 108 | + return source.toDate(); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private static class DateToDateTimeConverter implements Converter<Date, DateTime> { |
| 113 | + public DateTime convert(Date source) { |
| 114 | + return new DateTime(source); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private static class DateTimeToCalendarConverter implements Converter<DateTime, Calendar> { |
| 119 | + public Calendar convert(DateTime source) { |
| 120 | + return source.toGregorianCalendar(); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private static class CalendarToDateTimeConverter implements Converter<Calendar, DateTime> { |
| 125 | + public DateTime convert(Calendar source) { |
| 126 | + return new DateTime(source); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments