1 | /* |
2 | * Copyright 2013 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 | /** |
28 | * DateTimeConverters |
29 | * |
30 | * @author Rizwan Idrees |
31 | * @author Mohsin Husen |
32 | */ |
33 | |
34 | public final class DateTimeConverters { |
35 | |
36 | private static DateTimeFormatter formatter = ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC); |
37 | |
38 | public enum JodaDateTimeConverter implements Converter<ReadableInstant, String> { |
39 | INSTANCE; |
40 | |
41 | @Override |
42 | public String convert(ReadableInstant source) { |
43 | if (source == null) { |
44 | return null; |
45 | } |
46 | return formatter.print(source); |
47 | } |
48 | |
49 | } |
50 | |
51 | public enum JodaLocalDateTimeConverter implements Converter<LocalDateTime, String> { |
52 | INSTANCE; |
53 | |
54 | @Override |
55 | public String convert(LocalDateTime source) { |
56 | if (source == null) { |
57 | return null; |
58 | } |
59 | return formatter.print(source.toDateTime(DateTimeZone.UTC)); |
60 | } |
61 | |
62 | } |
63 | |
64 | public enum JavaDateConverter implements Converter<Date, String> { |
65 | INSTANCE; |
66 | |
67 | @Override |
68 | public String convert(Date source) { |
69 | if (source == null) { |
70 | return null; |
71 | } |
72 | |
73 | return formatter.print(source.getTime()); |
74 | } |
75 | |
76 | } |
77 | |
78 | } |