on 2020 Jun 26 4:43 PM
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
Request clarification before answering.
Hello bharatbajaj
Maybe this comment will help you: https://blogs.sap.com/2017/04/18/abap-unix-timestamp/#comment-373331
Kind regards,You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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,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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
| User | Count |
|---|---|
| 13 | |
| 7 | |
| 6 | |
| 5 | |
| 4 | |
| 4 | |
| 3 | |
| 3 | |
| 3 | |
| 3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.