'DateConversion is giving cast exception in Elastic search Query

I am trying to format date from '2022-03-14T15:34:11.125Z' to '2022-03-14'. I have tried below. It is giving Cast Exception.

{
    "query": {
        "script": {
            "script": {
                "source": "def dateString = doc['inputDate'];def inputFormat = new SimpleDateFormat(\"yyyy-MM-dd'T'HH:mm:ss\"); def convertedDate = inputFormat.parse(dateString);def outputFormat = new SimpleDateFormat('yyyy-MM-dd'); return outputFormat.format(convertedDate);",
                "lang": "painless"
            }
        }
    }
}

Exception details :

 "type": "script_exception",
                    "reason": "runtime error",
                    "script_stack": [
                        "convertedDate = inputFormat.parse(dateString);def ",
                        "                           ^---- HERE"
                    ],
                    "script": "def dateString = doc['inputDate'];def inputFormat = new SimpleDateFormat(\"yyyy-MM-dd'T'HH:mm:ss\"); def convertedDate = inputFormat.parse(dateString);def outputFormat = new SimpleDateFormat('yyyy-MM-dd'); return outputFormat.format(convertedDate);",
                    "lang": "painless",
                    "caused_by": {
                        "type": "class_cast_exception",
                        "reason": "class_cast_exception: Cannot cast org.elasticsearch.index.fielddata.ScriptDocValues$Dates to java.lang.String"                   

if i use .value getting below exception

def dateString = doc['UpdatedDate'].value;

{ "type": "class_cast_exception", "reason": "class_cast_exception: Cannot cast org.elasticsearch.script.JodaCompatibleZonedDateTime to java.lang.String" }




Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source