这两天接到一个新的需求,其中有一个日期格式转换的,主要用到一个系统类cl_abap_datfm,这里简单地写个测试程序记录一下!
选择界面就是一个日期输入框(显示样式由su3中设置的格式决定),和一个格式输入框,直接上程序:
*&---------------------------------------------------------------------*
*& Report ZGML_DATE
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zgml_date.
DATA: gv_xudatfm TYPE xudatfm,
gv_domval TYPE domvalue_l,
gv_text TYPE val_text,
gv_exdat TYPE char25,
gv_exdatfm TYPE xudatfm,
gv_msg TYPE string,
gv_date TYPE string.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
PARAMETERS: p_date TYPE sy-datum,
p_udatfm TYPE xudatfm OBLIGATORY.
SELECTION-SCREEN END OF BLOCK b1.
INITIALIZATION.
*get default date format.
CALL METHOD cl_abap_datfm=>get_datfm
RECEIVING
datfm = gv_xudatfm.
MOVE gv_xudatfm TO p_udatfm.
START-OF-SELECTION.
*check the date format entered
CLEAR: gv_xudatfm,gv_domval,gv_text.
CALL METHOD cl_abap_datfm=>get_datfm
RECEIVING
datfm = gv_xudatfm.
IF gv_xudatfm IS NOT INITIAL.
MOVE gv_xudatfm TO gv_domval.
CALL FUNCTION 'DOMAIN_VALUE_GET'
EXPORTING
i_domname = 'XUDATFM'
i_domvalue = gv_domval
IMPORTING
e_ddtext = gv_text
EXCEPTIONS
not_exist = 1
OTHERS = 2.
IF sy-subrc = 0.
WRITE :/ 'Default Date format',
AT 39(2) '=> ',
AT 44(10) gv_text.
ENDIF.
ENDIF.
IF p_udatfm IS NOT INITIAL.
MOVE p_udatfm TO gv_domval.
CALL FUNCTION 'DOMAIN_VALUE_GET'
EXPORTING
i_domname = 'XUDATFM'
i_domvalue = gv_domval
IMPORTING
e_ddtext = gv_text
EXCEPTIONS
not_exist = 1
OTHERS = 2.
IF sy-subrc = 0.
WRITE :/ 'Selected Date format',
AT 39(2) '=> ',
AT 44(10) gv_text.
ENDIF.
ENDIF.
WRITE :/ '----------------------------------------------------'.
CLEAR : gv_msg.
TRY.
CALL METHOD cl_abap_datfm=>conv_date_int_to_ext
EXPORTING
im_datint = p_date
im_datfmdes = p_udatfm
IMPORTING
ex_datext = gv_exdat
ex_datfmused = gv_exdatfm.
CATCH cx_abap_datfm_format_unknown .
MOVE 'Wrong Date format' TO gv_msg.
ENDTRY.
IF gv_msg IS NOT INITIAL.
WRITE :/ gv_msg.
ELSE.
MOVE p_date TO gv_date.
WRITE :/ 'Int format => ',
AT 15(10) gv_date,
AT 28(15) 'Ext Format => ',
AT 44(10) gv_exdat.
ENDIF.
p_date中显示的就是目前系统默认显示的日期格式: