@@ -154,51 +154,79 @@ private void initDateConverter() {
154
154
155
155
if (field != null && (field .type () == FieldType .Date || field .type () == FieldType .Date_Nanos )
156
156
&& (isTemporalAccessor || isDate )) {
157
- DateFormat dateFormat = field .format ();
157
+ DateFormat [] dateFormats = field .format ();
158
158
159
159
String property = getOwner ().getType ().getSimpleName () + "." + getName ();
160
160
161
- if (dateFormat == DateFormat . none ) {
161
+ if (dateFormats . length == 0 ) {
162
162
LOGGER .warn (
163
163
String .format ("No DateFormat defined for property %s. Make sure you have a Converter registered for %s" ,
164
164
property , actualType .getSimpleName ()));
165
165
return ;
166
166
}
167
167
168
- ElasticsearchDateConverter converter = null ;
168
+ List < ElasticsearchDateConverter > converters = new ArrayList <>() ;
169
169
170
- if ( dateFormat == DateFormat . custom ) {
171
- String pattern = field . pattern () ;
170
+ for ( int i = 0 ; i < dateFormats . length ; i ++ ) {
171
+ DateFormat dateFormat = dateFormats [ i ] ;
172
172
173
- if (!StringUtils .hasLength (pattern )) {
174
- throw new MappingException (
175
- String .format ("Property %s is annotated with FieldType.%s and a custom format but has no pattern defined" ,
176
- property , field .type ().name ()));
177
- }
173
+ if (dateFormat == DateFormat .custom ) {
174
+ String pattern = field .pattern ();
175
+
176
+ if (!StringUtils .hasLength (pattern )) {
177
+ throw new MappingException (String .format (
178
+ "Property %s is annotated with FieldType.%s and a custom format but has no pattern defined" , property ,
179
+ field .type ().name ()));
180
+ }
178
181
179
- converter = ElasticsearchDateConverter .of (pattern );
180
- } else {
181
-
182
- switch (dateFormat ) {
183
- case weekyear :
184
- case weekyear_week :
185
- case weekyear_week_day :
186
- LOGGER .warn ("no Converter available for " + actualType .getName () + " and date format " + dateFormat .name ()
187
- + ". Use a custom converter instead" );
188
- break ;
189
- default :
190
- converter = ElasticsearchDateConverter .of (dateFormat );
191
- break ;
182
+ converters .add (ElasticsearchDateConverter .of (pattern ));
183
+ } else {
184
+
185
+ switch (dateFormat ) {
186
+ case weekyear :
187
+ case weekyear_week :
188
+ case weekyear_week_day :
189
+ LOGGER .warn ("no Converter available for " + actualType .getName () + " and date format " + dateFormat .name ()
190
+ + ". Use a custom converter instead" );
191
+ break ;
192
+ default :
193
+ converters .add (ElasticsearchDateConverter .of (dateFormat ));
194
+ break ;
195
+ }
192
196
}
197
+
193
198
}
194
199
195
- if (converter != null ) {
196
- ElasticsearchDateConverter finalConverter = converter ;
200
+ if (!converters .isEmpty ()) {
197
201
propertyConverter = new ElasticsearchPersistentPropertyConverter () {
198
- final ElasticsearchDateConverter dateConverter = finalConverter ;
202
+ final List <ElasticsearchDateConverter > dateConverters = converters ;
203
+
204
+ @ SuppressWarnings ("unchecked" )
205
+ @ Override
206
+ public Object read (String s ) {
207
+ if (isTemporalAccessor ) {
208
+ for (ElasticsearchDateConverter dateConverter : dateConverters ) {
209
+ try {
210
+ return dateConverter .parse (s , (Class <? extends TemporalAccessor >) actualType );
211
+ } catch (Exception e ) {
212
+ }
213
+ }
214
+ } else { // must be date
215
+ for (ElasticsearchDateConverter dateConverter : dateConverters ) {
216
+ try {
217
+ return dateConverter .parse (s );
218
+ } catch (Exception e ) {
219
+ }
220
+ }
221
+ }
222
+
223
+ throw new RuntimeException (String
224
+ .format ("Unable to parse date value '%s' of property '%s' with configured converters" , s , property ));
225
+ }
199
226
200
227
@ Override
201
228
public String write (Object property ) {
229
+ ElasticsearchDateConverter dateConverter = dateConverters .get (0 );
202
230
if (isTemporalAccessor && TemporalAccessor .class .isAssignableFrom (property .getClass ())) {
203
231
return dateConverter .format ((TemporalAccessor ) property );
204
232
} else if (isDate && Date .class .isAssignableFrom (property .getClass ())) {
@@ -207,16 +235,6 @@ public String write(Object property) {
207
235
return property .toString ();
208
236
}
209
237
}
210
-
211
- @ SuppressWarnings ("unchecked" )
212
- @ Override
213
- public Object read (String s ) {
214
- if (isTemporalAccessor ) {
215
- return dateConverter .parse (s , (Class <? extends TemporalAccessor >) actualType );
216
- } else { // must be date
217
- return dateConverter .parse (s );
218
- }
219
- }
220
238
};
221
239
}
222
240
}
0 commit comments