DateFormatTransformer
Converts a date from its current format to a specified format.
Notes
This functionality allows you to formats a date from any given format to a format of choice, as per the formatting options found on SimpleDateFormat with the exception of the string EPOCH which represents the difference, measured in milliseconds, between the date and midnight, January 1, 1970. The default format for fromFormat or toFormat when not specified is EPOCH.
When omitting the toField, the value will replace the one in the same field name.
Multiple fromFormats values can be specified. Each formats will be tried in the order provided and the first format that succeed in parsing a date will be used. A date will be considered "bad" only if none of the formats could parse the date.
Storing values in an existing field
If a document already has a target field with the same name,
new values are appended to the existing list by default. Use onSet to manage this default behavior.
Parser Handler Compatibility
This functionality can be applied as either a pre-parse or post-parse handler.
Locale of Date Formatting
It is possible to specify a locale used for formatting dates. The locale is the ISO two-letter language code, with an optional ISO country code, separated with an underscore (e.g., fr for French, fr_CA for Canadian French). When no locale is specified, the default is en_US (US English).
Examples
The following converts a date that is sometimes obtained from the HTTP header "Last-Modified" and sometimes is an EPOCH date, into an Apache Solr date format
- YAML
- JSON
- XML
handler:
class: DateFormatTransformer
fromField: Last-Modified
toField: solr_date
toFormat: yyyy-MM-dd'T'HH:mm:ss.SSS'Z
fromFormats:
- EEE, dd MMM yyyy HH:mm:ss zzz
- EPOCH
{
"handler": {
"class": "DateFormatTransformer",
"fromField": "Last-Modified",
"toField": "solr_date",
"toFormat": "yyyy-MM-dd'T'HH:mm:ss.SSS'Z",
"fromFormats": [
"EEE, dd MMM yyyy HH:mm:ss zzz",
"EPOCH"
]
}
}
<handler>
<class>DateFormatTransformer</class>
<fromField>Last-Modified</fromField>
<toField>solr_date</toField>
<toFormat>yyyy-MM-dd'T'HH:mm:ss.SSS'Z</toFormat>
<fromFormats>
<fromFormat>EEE, dd MMM yyyy HH:mm:ss zzz</fromFormat>
<fromFormat>EPOCH</fromFormat>
</fromFormats>
</handler>
Usage
Full configuration skeleton, with every option and its default
- YAML
- JSON
- XML
class: DateFormatTransformer
fromField: string
fromFormats:
- string
fromLocale: string
keepBadDates: false
label: string
onSet: APPEND
toField: string
toFormat: string
toLocale: string
{
"class": "DateFormatTransformer",
"fromField": "string",
"fromFormats": [
"string"
],
"fromLocale": "string",
"keepBadDates": false,
"label": "string",
"onSet": "APPEND",
"toField": "string",
"toFormat": "string",
"toLocale": "string"
}
<handler>
<class>DateFormatTransformer</class>
<fromField>string</fromField>
<fromFormats>
<fromFormat>string</fromFormat>
</fromFormats>
<fromLocale>string</fromLocale>
<keepBadDates>false</keepBadDates>
<label>string</label>
<onSet>APPEND</onSet>
<toField>string</toField>
<toFormat>string</toFormat>
<toLocale>string</toLocale>
</handler>
Properties
| Property | Type | Required | Default |
|---|---|---|---|
fromField | string | No | - |
fromFormats | string[] | No | - |
fromLocale | string | No | - |
keepBadDates | boolean | No | false |
label | string | No | - |
onSet | enum | No | - |
toField | string | No | - |
toFormat | string | No | - |
toLocale | string | No | - |
Property Details
fromField
specifies the document field containing the date to be reformatted.
fromFormats
Specifies one or multiple format of the input date string to be reformatted.
fromLocale
Specifies the expected format(s) of the input date string(s) to be reformatted.
keepBadDates
Determines whether to retain the original date string if it cannot be parsed or reformatted.
label
An optional, user-supplied label for this step in the importer pipeline. It has no effect on processing — it exists purely to help identify this step, for example in large configurations or in the visual configurator.
onSet
What to do if a target field of the same name already exist.
Allowed Values
APPENDPREPENDREPLACEOPTIONAL
toField
A target field name used to store the value.
toFormat
Specifies the desired format to convert the input date string into.
toLocale
Defines the locale to apply when formatting the output date string.