cancel
Showing results for 
Search instead for 
Did you mean: 

Convert Unix / EPOCH time stamp into simple date

gabe_f
Explorer
0 Kudos
3,192

Hi All,

I have an Odata response in JSON format where the invoice date is passed as a timestamp. The standard JSON to XML convert function works well except the date.

<CGROSS_AMOUNT_CU>USD</CGROSS_AMOUNT_CU><br><CINVOICE_DATE>/Date(1645660800000)/</CINVOICE_DATE><br><CINVOICE_UUID>028</CINVOICE_UUID>

I have a requirement to covert /Date(1645660800000)/ to yyyy-MM-dd for an XML mapping. The date trans function within the message mapping didn't covert correctly. What is the easiest method? Thanks.

Accepted Solutions (0)

Answers (4)

Answers (4)

YuvrajKarwar
Explorer
0 Kudos

Hello Gabrielu.

Check below groovy script to convert EPOCH time stamp, it will directly give you date. You can manipulate time zone as per your requirment.

import com.sap.gateway.ip.core.customdev.util.Message;
    import java.util.*;
    import java.time.Instant;
    import java.time.LocalDate;
    import java.time.ZoneId;
    import java.time.ZonedDateTime;

    
    def Message processData(Message message) {
        map = message.getProperties();
        Instant i=Instant.ofEpochMilli(1645660800000)
        ZonedDateTime z=i.atZone(ZoneId.of("Asia/Calcutta"))
        LocalDate date=z.toLocalDate()
        message.setProperty("CINVOICE_DATE", date);
        return message;
        }
gabe_f
Explorer
0 Kudos

Thanks. But the script didn't convert though.

0 Kudos

Hello Gabrielu,

i had a similar problem a couple of days ago. The other answers mentioned groovy script. I solved it by using Javascript:

timestamp = timestamp * 1000;
var convertedTimestamp = new Date(timestamp);

"timestamp" here is the unix epoch. I then saved the converted timestamp as a property and used it in my mapping. You could definitely achieve this in groovy script as well if that is a requirement for you.

Sriprasadsbhat
Active Contributor
0 Kudos

Hello gabrielu,

Please refer below and you can write groovy script to perform the conversion.

#Leverage JSONSlurper to parse the input JSON ( for particular field )

#2 Write script with below logic.

Reference

Regards,

Sriprasad Shivaram Bhat

PriyankaChak
Active Contributor
0 Kudos

Hi Gabrielu,

For epoch to date conversion, you can use groovy script.

Regards,

Priyanka