cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Convert JSON Date from external OData to YYYYMMDD in ABAP Program

bharatbajaj
Active Participant
15,575

Hi SAP Experts,

I am calling an external OData Service in my SAP ECC System (Netweaver version 7.0) using CL_HTTP_CLIENT.

When I call the service, I get the Date in JSON format, which the Unix/Epoch Format : “\/Date(1567981296000)\/”

As per the Unix format this value means 01.01.1970 + 1567981296000 seconds.

I want to convert this date into a regular SAP date-time format (YYYYMMDD HH:MM:SS) in my ABAP Code.

However, I'm not able to find any readymade FM which can do this conversion and give me the real date.

Can someone please provide the solution for this?

Regards,

Bharat Bajaj

Accepted Solutions (1)

Accepted Solutions (1)

MateuszAdamus
Active Contributor

Hello bharatbajaj

Maybe this comment will help you: https://blogs.sap.com/2017/04/18/abap-unix-timestamp/#comment-373331

Kind regards,
Mateusz
bharatbajaj
Active Participant
0 Likes

Hi Mateusz,

thanks for sharing the link..unfortunately I am using a very old version of SAP ECC with netweaver 7.0, so this class cl_pco_utility does not exist in my system.

MateuszAdamus
Active Contributor

Hello bharatbajaj

This is the CONVERT_JAVA_TIMESTAMP_TO_ABAP method's body. I think you should be able to implement it on your system and then use the logic from the mentioned blog comment.

METHOD convert_java_timestamp_to_abap.
  DATA:
    lv_date        TYPE sy-datum,
    lv_days_i      TYPE i,
    lv_sec_i       TYPE i,
    lv_timestamp   TYPE timestampl,
    lv_timsmsec    TYPE timestampl.
  CONSTANTS:
    lc_day_in_sec TYPE i VALUE 86400.

* IV_TIMESTAMP stores milliseconds since January 1, 1970, 00:00:00 GMT
  lv_timestamp = iv_timestamp / 1000.   "timestamp in seconds
* One day has 86400 seconds: Timestamp in days
  lv_days_i    = lv_timestamp DIV lc_day_in_sec.
  lv_date      = '19700101'.
  ev_date     = lv_date + lv_days_i.
* Rest seconds (timestamp - days)
  lv_sec_i    = lv_timestamp MOD lc_day_in_sec.
  ev_time     = lv_sec_i.
* Rest sec and milli seconds
  lv_timsmsec  = lv_timestamp MOD lc_day_in_sec.
  lv_timsmsec  = lv_timsmsec - lv_sec_i.
  ev_msec      = lv_timsmsec * 1000.

ENDMETHOD.
Kind regards,
Mateusz
bharatbajaj
Active Participant
0 Likes

Hi mateuszadamus,

Thanks a lot for the piece of code. that worked perfectly.

regards,

Bharat Bajaj

Answers (2)

Answers (2)

bharatbajaj
Active Participant

Now I found a better way to handle the JSON DateTime value.

If I declare my ABAP field as:

LV_DATETIME TYPE TIMESTAMP, (instead of string)

the /ui2/cl_json class will automatically transform the JSON Date into abap Timestamp.

try this sample code :

TYPES : BEGIN OF lty_data,
          order_no     TYPE char10,
          create_date  TYPE timestamp,  " <<<<<<<<< This is the Trick
        END OF lty_data.

DATA: ls_data  TYPE lty_data,
      lv_json  TYPE /ui2/cl_json=>json.

lv_json = '{ "Order_No" : "0021232324" , "Create_Date" : "/Date(1596240000000)/" }'.

/ui2/cl_json=>deserialize(
  EXPORTING
    json        = lv_json             " JSON string
  CHANGING
    data        = ls_data ).          " Converted Data

After deserialize, you'll notice that ls_data-create_date is converted to 20200801000000.

Such a simple thing, yet extremely important.

Hope this helps.

Regards,

Bharat Bajaj

ricky_shaw
Contributor
0 Likes
Hi, I tried this but i am seeing 0 for ls_data-create_date..can you help?
Sandra_Rossi
Active Contributor

If you know the Simple Transformations/want to use one, there's a formatting option to convert the "ticks" -> See ABAP documentation - ST - Mapping Rules for Elementary Types.

bharatbajaj
Active Participant

thanks for sharing the link..unfortunately, I am using a very old version of SAP ECC with Netweaver 7.0, so the json transformation does not exist in my system.