Date formatting Using CL_ABAP_DATFM
Summary
This post include the Sample program using the class CL_ABAP_DATFM for doing basic date formating.
Author(s): Vinod Kumar T
Company: NIIT Technologies Limited
Created on: 07.08.2010
Author(s) Bio
Vinod Kumar is an ABAP Consultant working with NIIT Technologies Limited.
CL_ABAP_DATFM methods used in Sample Code
Method | Description |
GET_DATFM | To get currently active date format |
CHECK_DATE_FORMAT | Check the validity of the date format |
GET_NUM_OF_DATEFORMATS | Total Number of Date formats available in the system |
GET_DELIMITER | To get delimiter of selected date format |
GET_DATE_FORMAT_DES | Date format Describer |
GET_PERIOD_FORMAT_DES | Period format Describer |
CONV_DATE_INT_TO_SHORTEX | Convert Internal Date to Abbrevated date without Year |
CONV_DATE_EXT_TO_INT | Convert External Date to Internal Date |
CONV_DATE_INT_TO_EXT | Convert Internal Date to External Date |
CONV_PERIOD_EXT_TO_INT | Convert External Period to Internal Period |
CONV_PERIOD_INT_TO_EXT | Convert Internal Period to External Period |
Input Screen
Sample Code
*&---------------------------------------------------------------------*
*& Report ZVK_CL_ABAP
*&---------------------------------------------------------------------*
*& Sample program to demonstrate methods in class : CL_ABAP_DATFM
*&---------------------------------------------------------------------*
** Text elements
* p_date : date
* p_udatfm : Date format
REPORT zvk_cl_abap.
TYPE-POOLS : abap.
TABLES : likp.
DATA : lv_re_num TYPE int4, "No of date formats"
lv_xudatfm TYPE xudatfm, "Date format"
lv_domval TYPE domvalue_l, "Date format for FM"
lv_text TYPE val_text, "Date format description"
lv_delim TYPE char1, "Date delimiter"
lv_format TYPE char25, "Date format"
lv_exdat TYPE char25, "External Date"
lv_exdatfm TYPE xudatfm. "Date format"
DATA : lv_flag TYPE abap_bool, "Flag (True/False)"
lv_valid TYPE abap_bool, "Flag for Date format validity"
lv_date1 TYPE d. "Date"
DATA : lv_msg TYPE string, "Message"
lv_date_st TYPE string. "Date in String"
DATA : lv_per_int TYPE char25, "Internal Period"
lv_per_ext TYPE char25. "External Period"
DATA : lv_dat_sht TYPE char25. "Short Date without year"
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
PARAMETER : p_date TYPE sy-datum.
PARAMETER : p_udatfm TYPE xudatfm OBLIGATORY.
SELECTION-SCREEN END OF BLOCK b1.
INITIALIZATION.
** Populate default date format.
CALL METHOD cl_abap_datfm=>get_datfm
RECEIVING
datfm = lv_xudatfm.
MOVE lv_xudatfm TO p_udatfm.
START-OF-SELECTION.
************************************************************************
** Check the date format entered
************************************************************************
CLEAR : lv_valid.
CALL METHOD cl_abap_datfm=>check_date_format
EXPORTING
im_datfm = p_udatfm
RECEIVING
re_is_valid = lv_valid.
IF lv_valid NE abap_true.
MESSAGE 'invalid date format' TYPE 'I' DISPLAY LIKE 'E'.
STOP.
ENDIF.
************************************************************************
** Method to display Default date format
************************************************************************
CLEAR : lv_xudatfm, lv_text, lv_re_num,
lv_domval.
CLEAR : lv_per_int.
MOVE p_date+0(6) TO lv_per_int.
CALL METHOD cl_abap_datfm=>get_datfm
RECEIVING
datfm = lv_xudatfm.
IF lv_xudatfm IS NOT INITIAL.
MOVE lv_xudatfm TO lv_domval.
CALL FUNCTION 'DOMAIN_VALUE_GET'
EXPORTING
i_domname = 'XUDATFM'
i_domvalue = lv_domval
IMPORTING
e_ddtext = lv_text
EXCEPTIONS
not_exist = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ELSE.
WRITE :/ 'Default Date format',
AT 39(2) '=> ',
AT 44(10) lv_text.
ENDIF.
ENDIF.
IF p_udatfm IS NOT INITIAL.
MOVE p_udatfm TO lv_domval.
CALL FUNCTION 'DOMAIN_VALUE_GET'
EXPORTING
i_domname = 'XUDATFM'
i_domvalue = lv_domval
IMPORTING
e_ddtext = lv_text
EXCEPTIONS
not_exist = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ELSE.
WRITE :/ 'Selected Date format',
AT 39(2) '=> ',
AT 44(10) lv_text.
ENDIF.
ENDIF.
************************************************************************
** Method to display total number of date formats available
************************************************************************
WRITE :/ '----------------------------------------------------'.
CALL METHOD cl_abap_datfm=>get_num_of_dateformats
RECEIVING
re_num = lv_re_num.
WRITE :/ 'Number of Date formats',
AT 39(2) '=> ',
AT 44(10) lv_re_num.
************************************************************************
** Method to disaply the delimiter in Selected date format
************************************************************************
WRITE :/ '----------------------------------------------------'.
CLEAR : lv_delim, lv_msg.
TRY.
CALL METHOD cl_abap_datfm=>get_delimiter
EXPORTING
im_datfm = p_udatfm
IMPORTING
ex_delimiter = lv_delim.
CATCH cx_abap_datfm_format_unknown .
MOVE 'Unknown Date format' TO lv_msg.
ENDTRY.
IF lv_msg IS NOT INITIAL.
WRITE :/ 'Error in Fetching Date Delimiter :' , lv_msg.
ELSE.
WRITE :/ 'Date delimiter ' ,
AT 39(2) '=> ',
AT 44(10) lv_delim.
ENDIF.
************************************************************************
** Method to display Date format description
************************************************************************
WRITE :/ '----------------------------------------------------'.
CLEAR : lv_format, lv_msg.
TRY.
CALL METHOD cl_abap_datfm=>get_date_format_des
EXPORTING
im_datfm = p_udatfm
im_langu = sy-langu
im_plain = abap_true
IMPORTING
ex_dateformat = lv_format.
CATCH cx_abap_datfm_format_unknown .
MOVE 'Unknown Date format' TO lv_msg.
ENDTRY.
IF lv_msg IS NOT INITIAL.
WRITE :/ 'Error in Fetching Format Description :' , lv_msg.
ELSE.
WRITE :/ 'Date format (Plain)',
AT 39(2) '=> ',
AT 44(10) lv_format.
ENDIF.
CLEAR : lv_format, lv_msg.
TRY.
CALL METHOD cl_abap_datfm=>get_date_format_des
EXPORTING
im_datfm = p_udatfm
im_langu = sy-langu
im_plain = abap_false
IMPORTING
ex_dateformat = lv_format.
CATCH cx_abap_datfm_format_unknown .
MOVE 'Unknown Date format' TO lv_msg.
ENDTRY.
IF lv_msg IS NOT INITIAL.
WRITE :/ 'Error in Fetching Format Description :' , lv_msg.
ELSE.
WRITE :/ 'Date format ',
AT 39(2) '=> ',
AT 44(10) lv_format.
ENDIF.
************************************************************************
** Method to display period format description
************************************************************************
WRITE :/ '----------------------------------------------------'.
CLEAR : lv_format, lv_msg.
TRY.
CALL METHOD cl_abap_datfm=>get_period_format_des
EXPORTING
im_datfm = p_udatfm
im_langu = sy-langu
im_plain = abap_true
IMPORTING
ex_periodformat = lv_format.
CATCH cx_abap_datfm_format_unknown .
MOVE 'Unknown Date format' TO lv_msg.
ENDTRY.
IF lv_msg IS NOT INITIAL.
WRITE :/ 'Error in Fetching Format Description :' , lv_msg.
ELSE.
WRITE :/ 'Period format(Plain)',
AT 39(2) '=> ',
AT 44(10) lv_format.
ENDIF.
CLEAR : lv_format, lv_msg.
TRY.
CALL METHOD cl_abap_datfm=>get_period_format_des
EXPORTING
im_datfm = p_udatfm
im_langu = sy-langu
im_plain = abap_false
IMPORTING
ex_periodformat = lv_format.
CATCH cx_abap_datfm_format_unknown .
MOVE 'Unknown Date format' TO lv_msg.
ENDTRY.
IF lv_msg IS NOT INITIAL.
WRITE :/ 'Error in Fetching Format Description :' , lv_msg.
ELSE.
WRITE :/ 'Period format',
AT 39(2) '=> ',
AT 44(10) lv_format.
ENDIF.
************************************************************************
** Method to conver Internal Date to External date format
************************************************************************
WRITE :/ '----------------------------------------------------'.
CLEAR : lv_msg.
TRY.
CALL METHOD cl_abap_datfm=>conv_date_int_to_ext
EXPORTING
im_datint = p_date
im_datfmdes = p_udatfm
IMPORTING
ex_datext = lv_exdat
ex_datfmused = lv_exdatfm.
CATCH cx_abap_datfm_format_unknown .
MOVE 'Wrong Date format' TO lv_msg.
ENDTRY.
IF lv_msg IS NOT INITIAL.
WRITE :/ lv_msg.
ELSE.
MOVE p_date TO lv_date_st.
WRITE :/ 'Int format => ',
AT 15(10) lv_date_st,
AT 28(15) 'Ext Format => ',
AT 44(10) lv_exdat.
ENDIF.
************************************************************************
** Method to convert External Date to internal date format
************************************************************************
WRITE :/ '----------------------------------------------------'.
CLEAR : lv_msg.
TRY.
CALL METHOD cl_abap_datfm=>conv_date_ext_to_int
EXPORTING
im_datext = lv_exdat
im_datfmdes = p_udatfm
IMPORTING
ex_datint = lv_date1.
CATCH cx_abap_datfm_no_date .
MOVE 'No Date' TO lv_msg.
CATCH cx_abap_datfm_invalid_date .
MOVE 'Invlid Date' TO lv_msg.
CATCH cx_abap_datfm_format_unknown .
MOVE 'Unknown Date format' TO lv_msg.
CATCH cx_abap_datfm_ambiguous .
MOVE 'Ambiguous Date' TO lv_msg.
ENDTRY.
IF lv_msg IS NOT INITIAL.
WRITE :/ 'Error :' , lv_msg.
ELSE.
MOVE lv_date1 TO lv_date_st.
WRITE :/ 'Ext.Format => ',
AT 15(10) lv_exdat,
AT 28(15) 'Int.Format => ',
AT 44(10) lv_date_st.
ENDIF.
************************************************************************
** Method to convert Internal date to short date (dd.mm - without year).
************************************************************************
WRITE :/ '----------------------------------------------------'.
CLEAR : lv_msg, lv_dat_sht, lv_exdatfm, lv_date_st.
TRY.
CALL METHOD cl_abap_datfm=>conv_date_int_to_shortex
EXPORTING
im_datint = p_date
im_datfmdes = p_udatfm
IMPORTING
ex_abbrdatext = lv_dat_sht
ex_datfmused = lv_exdatfm.
CATCH cx_abap_datfm_format_unknown .
MOVE 'Invalid Date format' TO lv_msg.
ENDTRY.
IF lv_msg IS NOT INITIAL.
WRITE :/ 'Error :' , lv_msg.
ELSE.
MOVE p_date TO lv_date_st.
WRITE :/ 'Int. Date => ',
AT 15(10) lv_date_st,
AT 28(15) 'Short Date => ',
AT 44(10) lv_dat_sht.
ENDIF.
************************************************************************
** Method to convert Internal Period format to External Period Format
************************************************************************
WRITE :/ '----------------------------------------------------'.
CLEAR : lv_msg.
TRY.
CALL METHOD cl_abap_datfm=>conv_period_int_to_ext
EXPORTING
im_periodint = lv_per_int
im_datfmdes = p_udatfm
IMPORTING
ex_periodext = lv_per_ext
ex_datfmused = lv_exdatfm.
CATCH cx_abap_datfm_format_unknown .
MOVE 'Invalid Date format' TO lv_msg.
ENDTRY.
IF lv_msg IS NOT INITIAL.
WRITE :/ 'Error :' , lv_msg.
ELSE.
MOVE p_date TO lv_date_st.
WRITE :/ 'Int.period=> ',
AT 15(10) lv_per_int,
AT 28(15) 'Ext.Period=> ',
AT 44(10) lv_per_ext.
ENDIF.
************************************************************************
** Method to convert External Period format to Interal Period Format.
************************************************************************
WRITE :/ '----------------------------------------------------'.
CLEAR : lv_msg, lv_per_int.
TRY.
CALL METHOD cl_abap_datfm=>conv_period_ext_to_int
EXPORTING
im_periodext = lv_per_ext
im_datfmdes = p_udatfm
im_inweeks = abap_true
IMPORTING
ex_periodint = lv_per_int
ex_datfmused = lv_exdatfm.
CATCH cx_abap_datfm_no_date .
MOVE 'No Date Passed' TO lv_msg.
CATCH cx_abap_datfm_invalid_date .
MOVE 'Invalid Date' TO lv_msg.
CATCH cx_abap_datfm_format_unknown .
MOVE 'Invalid Date format' TO lv_msg.
CATCH cx_abap_datfm_ambiguous .
MOVE 'Ambiguous Date' TO lv_msg.
ENDTRY.
IF lv_msg IS NOT INITIAL.
WRITE :/ 'Error :' , lv_msg.
ELSE.
MOVE p_date TO lv_date_st.
WRITE :/ 'Ext.period=> ',
AT 15(10) lv_per_ext,
AT 28(15) 'Int.Period=> ',
AT 44(10) lv_per_int.
ENDIF.
Report Output