Java教程

ABAP-时间函数

本文主要是介绍ABAP-时间函数,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

获取两个时间之间的分钟数

data min TYPE i.

CALL FUNCTION 'DELTA_TIME_DAY_HOUR'
  EXPORTING
    T1            = '060000'
    T2            = '070000'
    D1            = '20160101'
    D2            = '20160101'
 IMPORTING
   MINUTES        = min.

write min.

获取两个日期期间的:天数、周数、月数、年数

包括期间月份的开始(月份第一天日期)、截止日期(月份最后一天日期)

TYPE-POOLS: p99sg.
DATA: days   TYPE i,
      weeks  TYPE i,
      months TYPE i,
      years  TYPE i.
 
DATA: month_tab TYPE STANDARD TABLE OF p99sg_month_tab_row.
 
CALL FUNCTION 'HR_99S_INTERVAL_BETWEEN_DATES'
  EXPORTING
    begda     = '20150101'    "开始日期
    endda     = '20160101'    "截止日期
  IMPORTING
    days      = days          "返回天数:266
    c_weeks   = weeks         "返回周数:52
    c_months  = months        "返回月数:12
    c_years   = years         "返回年数:1
    month_tab = month_tab.    "期间月份首尾日

通过函数计算两个日期的差异月份

CALL FUNCTION 'MONTHS_BETWEEN_TWO_DATES_NEW'
        EXPORTING
          I_DATUM_BIS             = 
          I_DATUM_VON             = 
*       I_KZ_INCL_BIS           = ' '
*       I_KZ_VOLLE_MONATE       = 'X'
       IMPORTING
         E_MONATE                =

获取输入日期月份的最后一天的日期

CALL FUNCTION 'LAST_DAY_OF_MONTHS'
  EXPORTING
    day_in            = '20160104'     "输入日期
  IMPORTING
    last_day_of_month =            "返回最后一天日期:20160131
  EXCEPTIONS
    day_in_no_date    = 1
    OTHERS            = 2.
CALL FUNCTION 'LAST_DAY_OF_MONTHS'
  EXPORTING
    day_in            = '20160104'     "输入日期
  IMPORTING
    last_day_of_month =            "返回最后一天日期:20160131
  EXCEPTIONS
    day_in_no_date    = 1
    OTHERS            = 2.
CALL FUNCTION 'BKK_GET_MONTH_LASTDAY'
  EXPORTING
    i_date = '20160101'   "输入日期
  IMPORTING
    e_date = date. 20160131

根据条件获取需要的日期

CALL FUNCTION 'RP_CALC_DATE_IN_INTERVAL'
  EXPORTING
    date      = '20160101'    "日期
    days      = 10            "天数
    months    = 0             "月数
    signum    = '+'           
    years     = 0             "年数
  IMPORTING
    calc_date = calc_date.    (2016.01.11)
CALL FUNCTION 'FIMA_DATE_CREATE'
  EXPORTING
    I_DATE                        = '20160101'
   I_FLG_END_OF_MONTH            = ''
   I_YEARS                       = 1
*   I_MONTHS                      = 0
   I_DAYS                        = 1
*   I_CALENDAR_DAYS               = 2
  " I_SET_LAST_DAY_OF_MONTH       = 'X'
 IMPORTING
   E_DATE                        = w_date
   E_FLG_END_OF_MONTH            = w_flag
*   E_DAYS_OF_I_DATE              = w_days
          .
write:  w_date,
      / w_flag,
      / w_days.
DATA date TYPE sy-datum.
 
CALL FUNCTION 'MONTH_PLUS_DETERMINE'
  EXPORTING
    months  = 3
    olddate = '20140101'   "输入日期
  IMPORTING
    newdate = date.        "返回日期:20140401
DATA date TYPE sy-datum.
 
CALL FUNCTION 'CCM_GO_BACK_MONTHS'
  EXPORTING
    currdate   = '20140101'   "输入日期
    backmonths = 3            "过去月数
  IMPORTING
    newdate    = date.        "返回日期:20131001

判断是否为假日

CALL FUNCTION 'HOLIDAY_CHECK_AND_GET_INFO'
  EXPORTING
    date = '20160101'  "输入日期:春节
    holiday_calendar_id          = ''        "假日日历 ID
    with_holiday_attributes      = ' '         "属性标识
  IMPORTING
    holiday_found                = www     "输出是否为假日的标记  输出结果:X
  TABLES
    holiday_attributes           = attributes  "输出该假日的属性
                                               "若WITH_HOLIDAY_ATTRIBUTES参数 = X,则会带出属性记录
  EXCEPTIONS
    calendar_buffer_not_loadable = 1
    date_after_range             = 2
    date_before_range            = 3
    date_invalid                 = 4
    holiday_calendar_id_missing  = 5
    holiday_calendar_not_found   = 6
    OTHERS                       = 7.
CALL FUNCTION 'HOLIDAY_CHECK_AND_GET_INFO'
            EXPORTING
              date                = <lfs_tab>-datum
              holiday_calendar_id = 'CN'
            IMPORTING
              holiday_found       = v_cind.

          IF v_cind = ''.
            CALL FUNCTION 'DAY_IN_WEEK'
              EXPORTING
                datum = <lfs_tab>-datum
              IMPORTING
                wotnr = v_wotnr.
            CASE v_wotnr.
              WHEN 6 OR 7.
                v_cind = 'X'.
            ENDCASE.
          ENDIF.

转换日期为内部数字格式

CALL FUNCTION 'CONVERT_DATE_TO_INTERNAL'
  EXPORTING
    date_external            = '2016.01.01' "当前用户日期格式:YYYY.MM.DD
    accept_initial_date      = ' '
  IMPORTING
    date_internal            = date         "输出20160101
  EXCEPTIONS
    date_external_is_invalid = 1
    OTHERS                   = 2.

转换日期为系统日期格式

CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL'
  EXPORTING
    date_internal            = '20160101'  
  IMPORTING
    date_external            =                        2016.01.01
  EXCEPTIONS
    date_internal_is_invalid = 1
    OTHERS                   = 2.

查看日期属性

DATA: year_of_valid_from TYPE scal-year,
      year_of_valid_to   TYPE scal-year,
      return_code        TYPE sy-subrc.
 
DATA day_attributes TYPE STANDARD TABLE OF casdayattr.
 
CALL FUNCTION 'DAY_ATTRIBUTES_GET'
  EXPORTING
    factory_calendar           = ' '              "工厂日历ID
    holiday_calendar           = ' '              "假期日历ID
    date_from                  = '20140101'       "开始日期
    date_to                    = '20140110'       "截止日期
    language                   = sy-langu         "系统当前语言
    non_iso                    = ' '
  IMPORTING
    year_of_valid_from         = year_of_valid_from
    year_of_valid_to           = year_of_valid_to
    returncode                 = return_code
  TABLES
    day_attributes             = day_attributes "返回日期每日属性
  EXCEPTIONS
    factory_calendar_not_found = 1
    holiday_calendar_not_found = 2
    date_has_invalid_format    = 3
    date_inconsistency         = 4
    OTHERS                     = 5.

获取当前日期是星期几

DATA wotnr TYPE p.
 
CALL FUNCTION 'DAY_IN_WEEK'
  EXPORTING
    datum = '20140101'  "输入日期
  IMPORTING
    wotnr = wotnr.      "返回:3 =》星期三/周三

获取当前日期是第多少周

CALL FUNCTION 'DATE_GET_WEEK'
   EXPORTING
     date               =   '20211108'
  IMPORTING
     WEEK               =   l_week            "202145
  EXCEPTIONS
    DATE_INVALID       = 1
    OTHERS             = 2
           .
 IF sy-subrc <> 0.
* Implement suitable error handling here
 ENDIF.

获取当前日期周周一周日的日期

CALL FUNCTION 'GET_WEEK_INFO_BASED_ON_DATE'
    EXPORTING
      date   = sy-datum   "'20140101'
    IMPORTING
      week   = week       "返回周数:201401
      monday = monday     "返回周一日期:2013.12.30
      sunday = sunday.    "返回周日日期:2014.01.05
DATA date TYPE sy-datum.
 
CALL FUNCTION 'WEEK_GET_FIRST_DAY'
  EXPORTING
    week         = '201401'   "输入周
  IMPORTING
    date         = date       "返回该周第一天(周一):2013.12.30
  EXCEPTIONS
    week_invalid = 1
    OTHERS       = 2.

F4_DATE 函数

PARAMETERS:p1(6) TYPE c. 
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p1. 
  DATA:l_date TYPE sy-datum. 
  CALL FUNCTION 'F4_DATE'
    EXPORTING
      date_for_first_month         = sy-datum
    IMPORTING
      select_date                  = l_date     "用户选择后返回的日期
    EXCEPTIONS
      calendar_buffer_not_loadable = 1
      date_after_range             = 2
      date_before_range            = 3
      date_invalid                 = 4
      factory_calendar_not_found   = 5
      holiday_calendar_not_found   = 6
      parameter_conflict           = 7
      OTHERS                       = 8.


F4_CLOCK 函数

PARAMETERS:p1(6) TYPE c.
 
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p1.
 
  DATA: l_time TYPE sy-uzeit.
 
  CALL FUNCTION 'F4_CLOCK'
    EXPORTING
      start_time    = sy-uzeit
      display       = ' '
    IMPORTING
      selected_time = l_time.

F4 弹出框的形式选择年月

PARAMETERS: p_year   TYPE mard-lfgja DEFAULT sy-datum+0(4),
            p_month  TYPE mard-lfmon.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_month.

  DATA: actual_month   LIKE isellist-month,
        selected_month LIKE isellist-month,
        return_code    TYPE sy-subrc.

  CONCATENATE p_year sy-datum+4(2) INTO actual_month.

  CALL FUNCTION 'POPUP_TO_SELECT_MONTH'
    EXPORTING
      actual_month               = actual_month   "传入年份
      factory_calendar           = ' '            "工厂日历 ID
      holiday_calendar           = ' '            "假日日历 ID
      language                   = sy-langu       "当前系统语言
      start_column               = 8              "弹出框屏幕位置
      start_row                  = 5              "弹出框屏幕位置
    IMPORTING
      selected_month             = selected_month "返回用户输入月份
      return_code                = return_code    "返回 sy-subrc
    EXCEPTIONS
      factory_calendar_not_found = 1
      holiday_calendar_not_found = 2
      month_not_found            = 3
      OTHERS                     = 4.

  IF return_code = 0.

    p_year  = selected_month+0(4).
    p_month = selected_month+4(2).

  ENDIF.

弹出选择周的对话框

DATA: begin_date TYPE sy-datum,
      end_date   TYPE sy-datum.
 
CALL FUNCTION 'POPUP_CALENDAR_SDB'
  EXPORTING
    sel_day      = 'X'        "启用天 选择
    sel_week     = 'X'        "启用周 选择
    sel_month    = 'X'        "启用月 选择
    sel_interval = 'X'
    focus_day    = sy-datum   "默认选择日期
  IMPORTING
    begin_date   = begin_date "返回选择周第一天
    end_date     = end_date.  "返回选择周最后一天

12 小时制与 24 小时制的时间转换

DATA: lv_in_time  TYPE tims,
      lv_out_time TYPE tims,
      lv_am_pm    TYPE c.
lv_in_time = '080000'.
CALL FUNCTION 'HRVE_CONVERT_TIME'
  EXPORTING
    type_time       = 'B'    " A = 24小时制 -> 12小时制  B = 12小时制 -> 24小时制
    input_time      = lv_in_time
    input_am_pm     = 'PM'
  IMPORTING
    output_time     = lv_out_time
    output_am_pm    = lv_am_pm
  EXCEPTIONS
    parameter_error = 1
    OTHERS          = 2.
 
WRITE:/ lv_in_time . 
WRITE:/ lv_out_time.

日期格式转换

CALL FUNCTION 'FORMAT_DATE_4_OUTPUT'
  EXPORTING
    DATIN         = 
    FORMAT        =
* IMPORTING
*   DATEX         =
          .
DATA date2(10) TYPE c.
 
CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL'
  EXPORTING
    date_internal            = '20140101'  
  IMPORTING
    date_external            = date2     "根据用户日期格式参数输出:YYYY.MM.DD
  EXCEPTIONS
    date_internal_is_invalid = 1
    OTHERS                   = 2.

时间戳

长度 15 的 DEC 类型,组成为  8 位日期 + 6 位时间。

把时间和日期转成时间戳

CALL FUNCTION 'CIF_GEN4_CONVERT_DATETIME'
   EXPORTING
     iv_date                      = l_datum1
     IV_TIME                      = l_time1
     IV_TIMEZONE                  = SY-ZONLO
   IMPORTING
     EV_TIMESTAMP                 = l_timestamp1
  EXCEPTIONS
    TIME_CONVERSION_FAILED       = 1
    OTHERS                       = 2
           .
CALL FUNCTION 'IB_CONVERT_INTO_TIMESTAMP'
  EXPORTING
    I_DATLO           =
    I_TIMLO           =
*   I_TZONE           = SY-ZONLO
* IMPORTING
*   E_TIMESTAMP       =
          .
REPORT  z_barry_test.
 
TABLES: t001w,adrc .
DATA: date LIKE sy-datum,
      time LIKE sy-uzeit,
      tstamp LIKE tzonref-tstamps.
 
SELECT SINGLE * FROM t001w WHERE werks = '0001'.
IF sy-subrc = 0.
  SELECT SINGLE * FROM adrc WHERE addrnumber = t001w-adrnr . "time_zone
  PERFORM date_to_timestamp USING '20080808' '080808' adrc-time_zone
              CHANGING tstamp.
  WRITE tstamp.
ENDIF.
IF NOT tstamp IS INITIAL.
  PERFORM timestamp_to_date USING tstamp adrc-time_zone
              CHANGING date time.
  WRITE / : date,time .
ENDIF.
 
*&---------------------------------------------------------------------*
*&      Form  date_to_timestamp
*&---------------------------------------------------------------------*
*       日期+时间+时区  转变为 时间戳
*----------------------------------------------------------------------*
FORM date_to_timestamp USING i_date LIKE sy-datum
                             i_time LIKE sy-uzeit
                             i_tzone LIKE  tzonref-tzone
                        CHANGING o_tstamp LIKE tzonref-tstamps  .
  CONVERT DATE i_date TIME i_time INTO TIME STAMP o_tstamp TIME ZONE i_tzone.
  CASE sy-subrc.
    WHEN 8.
      MESSAGE e000(oo) WITH '时区错'.
    WHEN 12.
      MESSAGE e000(oo) WITH '时间戳错误'.
    WHEN OTHERS.
  ENDCASE.
ENDFORM.                    "date_to_timestamp
 
*&---------------------------------------------------------------------*
*&      Form  TIMESTAMP_TO_DATE
*&---------------------------------------------------------------------*
*       时间戳+时区 转变为 日期和时间
*----------------------------------------------------------------------*
FORM timestamp_to_date USING   i_tstamp LIKE tzonref-tstamps
                               i_tzone LIKE  tzonref-tzone
                       CHANGING o_date LIKE  sy-datum
                                o_time LIKE  sy-uzeit.
  DATA: w_tzone LIKE tzonref-tzone.
  w_tzone = i_tzone.
  CONVERT TIME STAMP i_tstamp TIME ZONE w_tzone INTO DATE o_date TIME o_time.
  CASE sy-subrc.
    WHEN 8.
      MESSAGE e000(oo) WITH '时区错'.
    WHEN 12.
      MESSAGE e000(oo) WITH '时间戳错误'.
    WHEN OTHERS.
  ENDCASE.
ENDFORM.                    "TIMESTAMP_TO_DATE

转载于:https://www.cnblogs.com/mingdashu/p/5099283.html

这篇关于ABAP-时间函数的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!