ABAP学习(9):时间日期操作

ABAP的时间日期操作

    ABAP的日期类型D,是一个8位字符串;时间类型T,是6位字符串。日期实际是一个字符串,格式;‘YYYYMMDD’,可以通过字符串截取获取年月日字符子串。

示例:使用字符串截取出年份、月份等

"日期

DATA:temp_date(8) TYPE C.

temp_date = sy-datum.

WRITE:/ temp_date.

temp_date = temp_date+2(2).

WRITE:/ temp_date.

 

1、判断日期是否在指定范围

示例:

WRITE:/'测试日期是否在一个时间段中'.

DATA:date1 type d,

     date2 type d.

date1 = '20181201'.

date2 = '20191221'.

 

if sy-datum > date1 and sy-datum < date2.

  WRITE:/ sy-datum,'在范围内'.

else.

  WRITE:/ sy-datum,'不在范围内'.

ENDIF.

 

2、判断日期是星期几

使用function,’DAY_IN_WEEK’判断一个日期是星期几

示例:

WRITE:'根据日期获取日期星期几'.

DATA: wotnr type p.

CALL FUNCTION 'DAY_IN_WEEK'

  EXPORTING

    DATUM         = sy-datum

 IMPORTING

   WOTNR         = wotnr.

WRITE:/ sy-datum,'是星期',wotnr.

 

3、根据日期获取日期所在周数

使用function,’DATE_GET_WEEK’,获取日期所在周数

示例:

CALL FUNCTION 'DATE_GET_WEEK'

  EXPORTING

    DATE               = sy-datum

 IMPORTING

   WEEK               = week

 

4、根据周数获取本周第一天日期

示例:

WRITE:/ '测试WEEK_GET_FIRST_DAY ,通过周数获取,本周第一天'.

DATA: date3 type d.

CALL FUNCTION 'WEEK_GET_FIRST_DAY'

  EXPORTING

    WEEK               = week

 IMPORTING

   DATE               = date3

 

5、检查日期格式是否正确

示例:

WRITE:/ '测试DATE_CHECK_FAILED ,检查日期是否正确'.

data: date4 type d.

data: date5 type d.

data: date6 type d.

 

date4 = '20101231'.    "需要这种格式日期

date5 = '12/23/2019'.  "不能识别

date6 = '12312010'.    "不能识别

CALL FUNCTION 'DATE_CHECK_PLAUSIBILITY'

  EXPORTING

    DATE                            = date6

 EXCEPTIONS

   PLAUSIBILITY_CHECK_FAILED       = 1

   OTHERS                          = 2.

 

6、根据输入日期获取所在月最后一天

示例:

WRITE:/ '测试RP_LAST_DAY_OF_MONTHS,获取输入日期所在月最后一天日期'.

data:date7 type d.

CALL FUNCTION 'RP_LAST_DAY_OF_MONTHS'

  EXPORTING

    DAY_IN                  = sy-datum

 IMPORTING

   LAST_DAY_OF_MONTH       = date7

 EXCEPTIONS

    DAY_IN_NO_DATE          = 1.

*   OTHERS                  = 2

 

7、将输入日期进行计算

示例:

WRITE:/'测试RP_CALC_DATE_IN_INTERVAL ,日期加减 年,月,日 计算'.

data: date8 type d value '20181201'.

data: date9 type d.

CALL FUNCTION 'RP_CALC_DATE_IN_INTERVAL'

  EXPORTING

    DATE            = date8

    DAYS            = 20   "

    MONTHS          = 0    "

    SIGNUM          = '-'  "运算符号

    YEARS           = 1    "

 IMPORTING

   CALC_DATE       = date9.

WRITE:/ date9.

方式2:日期计算函数

"日期計算
FORM f_test_cal_date.
  DATA:lv_rectype TYPE alpdbrtyp.
  DATA:lv_nbr_dwxmqy TYPE int4.
  DATA:lv_date TYPE datum.
  DATA:lv_redate TYPE datum.
  lv_date = sy-datum.

"rectypes:
"'Z':返回000000
"'I':day,
"'R':day,
"'D':day,
"'H':day,

"'A':week,
"'B':week,
"'W':week,
"'C':week,

"'J':Month,
"'K':Month,
"'M':Month,
"'L':Month,

"'N':quart,
"'O':quart,
"'Q':quart,
"'P':quart,

"'S':Year,
"'T':Year,
"'Y':Year,
"'U':Year,
*
"'E':xdays,
"'F':xdays,
"'X':xdays,
"'G':xdays,
**some special rectypes, only for reporting
*PERFDB_AVERAGE_TYP_DAY_5MIN TYPE ALPERFDB-RECTYPE VALUE '0',   "new
*PERFDB_AVERAGE_TYP_WEEK_5MIN TYPE ALPERFDB-RECTYPE VALUE '1',   "new
*PERFDB_AVERAGE_TYP_MONTH_5MIN TYPE ALPERFDB-RECTYPE VALUE '3',   "new
*PERFDB_AVERAGE_TYP_QUART_5MIN TYPE ALPERFDB-RECTYPE VALUE '4',   "new
*PERFDB_AVERAGE_TYP_YEAR_5MIN TYPE ALPERFDB-RECTYPE VALUE '5',   "new
*PERFDB_AVERAGE_TYP_XDAYS_5MIN TYPE ALPERFDB-RECTYPE VALUE '2',   "new
  TYPES:BEGIN OF s_cal_date,
          rectype TYPE char6,
          num TYPE int4,
        END OF s_cal_date.
  DATA:lt_cal_date TYPE TABLE OF s_cal_date.
  DATA:ls_cal_date LIKE LINE OF lt_cal_date.

  ls_cal_date-rectype = '5'.
  ls_cal_date-num = 1.
  APPEND ls_cal_date TO lt_cal_date.
  ls_cal_date-rectype = '0'.
  ls_cal_date-num = -1.
  APPEND ls_cal_date TO lt_cal_date.

  LOOP AT lt_cal_date INTO ls_cal_date.
    CALL FUNCTION 'SALP_CALC_DATE'
      EXPORTING
        IN_RECTYPE          = ls_cal_date-rectype
        IN_NBR_DWXMQY       = ls_cal_date-num
        IN_DATE             = lv_date
      IMPORTING
        OUT_DATE            = lv_date
      EXCEPTIONS
        OTHER_ERROR         = 1
        OTHERS              = 2.
    IF SY-SUBRC <> 0.
*   Implement suitable error handling here
    ENDIF.
  ENDLOOP.
ENDFORM.

 

8、获取时间和日期之差

示例:

WRITE:/ '测试SD_DATETIME_DIFFERENCE,获取日期,和时间之间差'.

DATA:date10 type d value '20180911'.

data:date11 type d value '20180922'.

DATA:date12 type p.

data:timediff type p.

 

CALL FUNCTION 'SD_DATETIME_DIFFERENCE'

  EXPORTING

    DATE1                  = date10

    TIME1                  = '080000'   "必须为 hhmmss格式

    DATE2                  = date11

    TIME2                  = '120000'

 IMPORTING

   DATEDIFF               = date12

   TIMEDIFF               = timediff.

 

9、获取每个月名字

示例:

WRITE:/ '获取每个月的名字'.

DATA: month_name like t247 OCCURS 0 WITH HEADER LINE.

CALL FUNCTION 'MONTH_NAMES_GET'

* EXPORTING

*   LANGUAGE                    = SY-LANGU

* IMPORTING

*   RETURN_CODE                 =

  TABLES

    MONTH_NAMES                 = month_name.

 

10、获取一段时间内节日日期

示例:

WRITE:/'获取节日表'.

DATA: holidays like iscal_day OCCURS 0 WITH HEADER LINE.

DATA: valid_year_from like SCAL-YEAR.

DATA: valid_year_to like SCAL-YEAR.

DATA: return_code like sy-subrc.

CALL FUNCTION 'HOLIDAY_GET'

 EXPORTING

    HOLIDAY_CALENDAR                 = 'CN'    "获取holiday  calendar id, tcode:scal

    FACTORY_CALENDAR                 = 'CN'   "获取工厂 calendar id ,通过tcode :SCAL

    DATE_FROM                        = '20180102'

    DATE_TO                          = '20181111'

 IMPORTING

   YEAR_OF_VALID_FROM               = valid_year_from

   YEAR_OF_VALID_TO                 = valid_year_to

   RETURNCODE                       = return_code

  TABLES

    HOLIDAYS                         = holidays.

 

11、判断日期是否是假日

    使用function,’HOLIDAY_CHECK_AND_GET_INFO’判断是否是假日,如果是返回值HOLIDAY_FOUND等于‘X’。

示例:

WRITE:'判断是否是假日'.

DATA:holiday_indicator type scal-indicator.

CALL FUNCTION 'HOLIDAY_CHECK_AND_GET_INFO'

  EXPORTING

    DATE                               = '20181001'

    HOLIDAY_CALENDAR_ID                = 'CN'

*   WITH_HOLIDAY_ATTRIBUTES            = ' '

  IMPORTING

    HOLIDAY_FOUND                      = holiday_indicator.

 

12、获取当前年月

示例:

WRITE:/'获取当前年份月份'.

DATA:cur_year like BKPF-GJAHR.

DATA:cur_month like BKPF-MONAT.

DATA:pre_year like BKPF-GJAHR.

DATA:pre_month like BKPF-MONAT.

CALL FUNCTION 'GET_CURRENT_YEAR'

  EXPORTING

    BUKRS         = 'BV02'

    DATE          = SY-DATUM

  IMPORTING

    CURRM         = cur_month

    CURRY         = cur_year

    PREVM         = pre_month

    PREVY         = pre_year

          .

WRITE:/'当前年月',cur_year,'--',cur_month.

WRITE:/'前一年月',pre_year,'--',pre_month.

 

posted @ 2019-11-16 08:20  渔歌晚唱  阅读(4628)  评论(2编辑  收藏  举报