SAP Tutorials Blog


 


"Using the CL_ABAP_DATFM class, you can perform conversions with external
"and internal representations of a time according to the date format, e.g.
"the conversion of a date in a data object of type string to type d and vice
"versa using a specific date format. Multiple methods are available, of which
"two are covered in the example. For more information, refer to the class
"documentation.
"Values of the date format for the conversion
"'1'  DD.MM.YYYY (Gregorian Date)
"'2'  MM/DD/YYYY (Gregorian Date)
"'3'  MM-DD-YYYY (Gregorian Date)
"'4'  YYYY.MM.DD (Gregorian Date)
"'5'  YYYY/MM/DD (Gregorian Date)
"'6'  YYYY-MM-DD (Gregorian Date, ISO 8601)
"'7'  GYY.MM.DD (Japanese Date)
"'8'  GYY/MM/DD (Japanese Date)
"'9'  GYY-MM-DD (Japanese Date)
"'A'  YYYY/MM/DD (Islamic Date 1)
"'B'  YYYY/MM/DD (Islamic Date 2)
"'C'  YYYY/MM/DD (Iranian Date)

"Data type and obect declarations for the example. The example, explores
"multiple date formats. For this purpose, each loop pass in a DO loop
"uses a different date format. First, the internal time format is converted
"to external time formats. In another loop, the conversions are performed
"the other way round. The results are stored in an internal table.
DATA(date4conversion) = CONV d( '20240202' ).
DATA conv_date_str TYPE string.
DATA conv_date_d TYPE d.
DATA date_format TYPE cl_abap_datfm=>ty_datfm.
TYPES: BEGIN OF date_ty_s,
          ext_date TYPE string,
          format   TYPE c LENGTH 1,
          int_date TYPE d,
        END OF date_ty_s.
DATA date_tab TYPE TABLE OF date_ty_s WITH EMPTY KEY.

"Conversion of d (internal) to string (external time format)
DO 7 TIMES.
  TRY.
      cl_abap_datfm=>conv_date_int_to_ext(
        EXPORTING im_datint    = date4conversion
                  im_datfmdes  = SWITCH #( sy-index
                                            WHEN 1 THEN cl_abap_datfm=>get_datfm( )
                                            WHEN 2 THEN '1'
                                            WHEN 3 THEN '2'
                                            WHEN 4 THEN '3'
                                            WHEN 5 THEN '6'
                                            WHEN 6 THEN 'A'
                                            WHEN 7 THEN 'C' )
        IMPORTING ex_datext    = conv_date_str
                  ex_datfmused = date_format  ).
      date_tab = VALUE #( BASE date_tab ( ext_date = conv_date_str format = date_format ) ).
    CATCH cx_abap_datfm_format_unknown.
  ENDTRY.
ENDDO.

"Conversion of string (external) to d (internal time format)
LOOP AT date_tab REFERENCE INTO DATA(ref_date).
  TRY.
      cl_abap_datfm=>conv_date_ext_to_int(
        EXPORTING im_datext    = ref_date->ext_date
                  im_datfmdes  = ref_date->format
        IMPORTING ex_datint    = conv_date_d
                  ex_datfmused = date_format ).
      ref_date->int_date = conv_date_d.
    CATCH cx_abap_datfm_no_date cx_abap_datfm_invalid_date 
          cx_abap_datfm_format_unknown cx_abap_datfm_ambiguous.
  ENDTRY.
ENDLOOP.

"Content of date_tab
"Note: The first entry in the table in your system may be different. The currently 
"active date format is retrieved.
"EXT_DATE      FORMAT    INT_DATE
"02/02/2024    2         20240202
"02.02.2024    1         20240202
"02/02/2024    2         20240202
"02-02-2024    3         20240202
"2024-02-02    6         20240202
"1445/07/22    A         20240202
"1402/11/13    C         20240202
"Note: When outputting the data object with the classrun,
"20240202, which is of type d, is displayed as 2014-02-02 in 
"the console for better readability.

"Getting the currently active date format
DATA(datfm) = cl_abap_datfm=>get_datfm( ).
"Getting the date format of a specific country
DATA(country_datfm) = cl_abap_datfm=>get_country_datfm( 'DE' ).

"Checking whether the date format is valid
DATA(is_valid) = cl_abap_datfm=>check_date_format( '1' ).
ASSERT is_valid = abap_true.

is_valid = cl_abap_datfm=>check_date_format( 'D' ).
ASSERT is_valid = abap_false.

Serkan AKKAVAK Computer Engineer BSc Head of SAP & Software Department Contact : serkurumsal@yandex.com