zoukankan      html  css  js  c++  java
  • 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.

    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.
  • 相关阅读:
    angularjs学习访问服务器(5)
    sublime text 3解放鼠标的快捷键总结
    AngularJS与服务器交互(4)
    android 串口调试
    QCom MSM MDP显示驱动一些点的简记
    如何提高android串口kernel log等级
    高通平台msm8909 LK 实现LCD 兼容
    MSM8909平台 LED背光的控制
    铝聚合物电池工作温度及低温电量差异的说明
    液晶屏工作稳定说明
  • 原文地址:https://www.cnblogs.com/tangToms/p/11870497.html
Copyright © 2011-2022 走看看