1 | /* |
2 | * Copyright 2012 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.data.elasticsearch.core.convert; |
17 | |
18 | import org.joda.time.DateTimeZone; |
19 | import org.joda.time.LocalDateTime; |
20 | import org.joda.time.ReadableInstant; |
21 | import org.joda.time.format.DateTimeFormatter; |
22 | import org.joda.time.format.ISODateTimeFormat; |
23 | import org.springframework.core.convert.converter.Converter; |
24 | |
25 | import java.util.Date; |
26 | |
27 | public final class DateTimeConverters { |
28 | |
29 | private static DateTimeFormatter formatter = ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC); |
30 | |
31 | public enum JodaDateTimeConverter implements Converter<ReadableInstant, String> { |
32 | INSTANCE; |
33 | |
34 | @Override |
35 | public String convert(ReadableInstant source) { |
36 | if (source == null) { |
37 | return null; |
38 | } |
39 | return formatter.print(source); |
40 | } |
41 | |
42 | } |
43 | |
44 | public enum JodaLocalDateTimeConverter implements Converter<LocalDateTime, String> { |
45 | INSTANCE; |
46 | |
47 | @Override |
48 | public String convert(LocalDateTime source) { |
49 | if (source == null) { |
50 | return null; |
51 | } |
52 | return formatter.print(source.toDateTime(DateTimeZone.UTC)); |
53 | } |
54 | |
55 | } |
56 | |
57 | public enum JavaDateConverter implements Converter<Date, String> { |
58 | INSTANCE; |
59 | |
60 | @Override |
61 | public String convert(Date source) { |
62 | if (source == null) { |
63 | return null; |
64 | } |
65 | |
66 | return formatter.print(source.getTime()); |
67 | } |
68 | |
69 | } |
70 | |
71 | } |