Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Work without FM

Former Member
0 Likes
1,192

Hi!

I have string, which the user enters himself.How find all sundays any month? Without different function and fm. And how convert string to date?

I would be glad for any help

6 REPLIES 6
Read only

Rodrigo-Giner
Active Contributor
0 Likes
1,097

Sorry I don't understand what you need and why you can't use FM

Regarding the conversion questions in ABAP the date (DATS) internally is a char lenght 8 (externally it's a char(10)) so you just have to pass the value in format YYYYMMDD into a DATS data type, no conversion is need.

Best Regards

Read only

GK817
Active Contributor
0 Likes
1,097

Hi Linda,

You can convert the string to DATE using the method suggested by Rodrigo above.

Then, you can pass that date to FM DATE_TO_DAY to get the day number. Day number 1 - Monday 2- Tuesday and so on. Based on the day, add number of days to get Sunday's date and then keep on adding 7 days to that date to get all Sundays' date.

Hope this helps.

Regards

GK

Read only

former_member1716
Active Contributor
0 Likes
1,097

Hello Linda Yan,

Please be clear and specific on your requirement, what will be your input and what is expected as output?

Read only

Former Member
0 Likes
1,097

Satish Kumar Balasubramanian ,I have 3 parameters.If you enter the word ‘date’ in field 3, you need to analyze field 1. If it records data in DD format.mm yyyy mm, display the dates of all Sundays of the given month, assuming that the first date should specify the first Sunday of the year.

Example: Field 1 = 06.01.2019 03

Field 3 = date

Conclusion: It is necessary to display the numbers of all Sundays for March 2019: 03 10 17 24 31

Read only

FredericGirod
Active Contributor
0 Likes
1,097

Name of the month -> T015M

(to avoid issue, translate in upper case)

In your requirement you are missing the year, how does the user enter it ?

To determine the day : date MOD 7

1 -> Monday ...

To count

identify the first day of the next month, something like that Date+0(4) && ( Date+4(2) + 1 ) && 01

Remove 1 until the month change (or the day = 01)

and make the MOD to determine if it is a SUNDAY

Read only

0 Likes
1,097

Here my proposal for the code of your exercise



PARAMETERS p_month TYPE char20.
PARAMETERS p_year  TYPE gjahr.





CLASS lc_month DEFINITION
      FINAL.


  PUBLIC SECTION.
    METHODS constructor
      IMPORTING
        iv_language TYPE sylangu OPTIONAL.
    METHODS convert_month_from_string
      IMPORTING
        iv_month               TYPE string
      RETURNING
        VALUE(rv_month_number) TYPE month.


  PRIVATE SECTION.
    METHODS get_all_month_names
      RETURNING
        VALUE(rt_month_names) TYPE char10_t.
    METHODS convert_upper_first_letter
      IMPORTING
        iv_word                  TYPE char10
      RETURNING
        VALUE(rv_word_converted) TYPE char10.
    DATA gv_language TYPE sylangu.


ENDCLASS.




CLASS lc_month IMPLEMENTATION.


  METHOD constructor.
    gv_language = COND #( WHEN iv_language IS NOT INITIAL THEN iv_language
                          ELSE sy-langu ).
  ENDMETHOD.


  METHOD convert_month_from_string.
    DATA(lv_month_converted) = convert_upper_first_letter( CONV char10( iv_month ) ).
    READ TABLE  get_all_month_names( )
         TRANSPORTING NO FIELDS
         WITH KEY table_line = lv_month_converted.
    IF sy-subrc EQ space.
      rv_month_number = sy-tabix.
    ENDIF.
  ENDMETHOD.


  METHOD convert_upper_first_letter.
    rv_word_converted = iv_word.
    CONDENSE rv_word_converted NO-GAPS.
    TRANSLATE rv_word_converted TO LOWER CASE.
    TRANSLATE rv_word_converted+0(1) TO UPPER CASE.
  ENDMETHOD.


  METHOD get_all_month_names.
    SELECT monam
           INTO TABLE rt_month_names
           FROM t015m
           WHERE spras EQ gv_language.
  ENDMETHOD.


ENDCLASS.






CLASS ltc_month DEFINITION
      FOR TESTING
      RISK LEVEL HARMLESS
      FINAL.
  PRIVATE SECTION.
    METHODS it_reads_camel_wording_month FOR TESTING.
    METHODS it_replies_0_for_unknown     FOR TESTING.
ENDCLASS.


CLASS ltc_month IMPLEMENTATION.


  METHOD it_reads_camel_wording_month.
    cl_abap_unit_assert=>assert_equals(
      act = NEW lc_month( 'E' )->convert_month_from_string( 'JaNuaRy' )
      exp = '01'
      msg = 'it should reply 01 for JaNuaRy').
  ENDMETHOD.


  METHOD it_replies_0_for_unknown.
    cl_abap_unit_assert=>assert_equals(
      act = NEW lc_month( 'E' )->convert_month_from_string( 'Apple' )
      exp = '00'
      msg = 'it should reply 00 for Apple').
  ENDMETHOD.




ENDCLASS.










CLASS lc_day DEFINITION
      FINAL.
  PUBLIC SECTION.
    CONSTANTS: BEGIN OF gc_day_type,
                 monday    TYPE i VALUE 1,
                 tuesday   TYPE i VALUE 2,
                 wednesday TYPE i VALUE 3,
                 thursday  TYPE i VALUE 4,
                 friday    TYPE i VALUE 5,
                 saturday  TYPE i VALUE 6,
                 sunday    TYPE i VALUE 7,
               END OF gc_day_type.
    METHODS get_numb_of_day_type_for_month
      IMPORTING
        iv_month                     TYPE month
        iv_year                      TYPE gjahr
        iv_day_type                  TYPE i
      RETURNING
        VALUE(rv_number_of_day_type) TYPE i.
  PRIVATE SECTION.
    METHODS get_last_day_of_month
      IMPORTING
        iv_month           TYPE month
        iv_year            TYPE gjahr
      RETURNING
        VALUE(rv_last_day) TYPE sydatum.
    METHODS get_first_day_of_month
      IMPORTING
        iv_month            TYPE month
        iv_year             TYPE gjahr
      RETURNING
        VALUE(rv_first_day) TYPE sydatum.
    METHODS is_date_a_day_type
      IMPORTING
        iv_date           TYPE sydatum
        iv_day_type       TYPE i
      RETURNING
        VALUE(rv_success) TYPE abap_bool.
ENDCLASS.




CLASS lc_day IMPLEMENTATION.


  METHOD get_numb_of_day_type_for_month.
    DATA(lv_calculation_date) = get_last_day_of_month( iv_month = iv_month
                                                       iv_year  = iv_year  ).
    DO.
      rv_number_of_day_type = COND #( WHEN is_date_a_day_type( iv_date = lv_calculation_date
                                                               iv_day_type = iv_day_type     )
                                        THEN rv_number_of_day_type + 1
                                      ELSE rv_number_of_day_type ).
      IF lv_calculation_date = get_first_day_of_month( iv_month = iv_month
                                                       iv_year  = iv_year  ) .
        EXIT.
      ENDIF.
      lv_calculation_date = lv_calculation_date - 1.


    ENDDO.
  ENDMETHOD.


  METHOD get_last_day_of_month.
    DATA(lv_month) = COND month( WHEN iv_month = 12 THEN 01
                                 ELSE iv_month + 1 ).
    DATA(lv_year)  = COND gjahr( WHEN lv_month = 01 THEN iv_year + 1
                                 ELSE iv_year ).
    rv_last_day+0(4) = lv_year.
    rv_last_day+4(2) = lv_month.
    rv_last_day+6(2) = '01'.
    rv_last_day = rv_last_day - 1.
  ENDMETHOD.


  METHOD is_date_a_day_type.
    rv_success = COND #( WHEN iv_day_type = ( iv_date MOD 7 ) THEN abap_true
                         ELSE abap_false ).
  ENDMETHOD.


  METHOD get_first_day_of_month.
    rv_first_day+0(4) = iv_year.
    rv_first_day+4(2) = iv_month.
    rv_first_day+6(2) = '01'.
  ENDMETHOD.


ENDCLASS.








CLASS ltc_day DEFINITION
      FOR TESTING
      RISK LEVEL HARMLESS
      FINAL.
  PRIVATE SECTION.
    METHODS it_returns_correct_nb_day_type FOR TESTING.
ENDCLASS.


CLASS ltc_day IMPLEMENTATION.


  METHOD it_returns_correct_nb_day_type.
    cl_abap_unit_assert=>assert_equals(
      act = NEW lc_day( )->get_numb_of_day_type_for_month( iv_month = '01'
                                                           iv_year  = '2019'
                                                           iv_day_type = lc_day=>gc_day_type-monday )
      exp = '04'
      msg = 'it should reply 04 for January 2019').
    cl_abap_unit_assert=>assert_equals(
      act = NEW lc_day( )->get_numb_of_day_type_for_month( iv_month = '12'
                                                           iv_year  = '2019'
                                                           iv_day_type = lc_day=>gc_day_type-monday )
      exp = '05'
      msg = 'it should reply 05 for January 2019').


  ENDMETHOD.






ENDCLASS.










START-OF-SELECTION.
  DATA(lo_month) = NEW lc_month( 'E' ).
  DATA(lo_day)   = NEW lc_day( ).
  WRITE /1 lo_day->get_numb_of_day_type_for_month( iv_month    = lo_month->convert_month_from_string( CONV string( p_month ) )
                                                   iv_year     = p_year
                                                   iv_day_type = lc_day=>gc_day_type-monday ).


END-OF-SELECTION.