SAP 日期时间处理总结

日期转换逻辑
 CALL FUNCTION ' CONVERSION_EXIT_INVDT_OUTPUT'
            EXPORTING
              input  = gs_exch_rate-gdatu  “789689”
            IMPORTING
              output = l_date.   “2021.09.08”
          SELECT SINGLE datfm FROM usr01 WHERE bname = @sy-uname INTO @DATA(lv_datfm).
          IF lv_datfm = '1'.
            CONCATENATE l_date+6(4) l_date+3(2) l_date(2) INTO gs_exch_rate-date.
          ELSEIF lv_datfm = '4'.
            CONCATENATE l_date+0(4) l_date+5(2) l_date+8(2) INTO gs_exch_rate-date.
          ENDIF.
把时间和日期转成时间戳
 CONVERT DATE IV_DATE  TIME IV_TIME
             INTO TIME STAMP EV_TIMESTAMP
                  TIME ZONE  SY-ZONLO.
把时间和日期转成时间 函数 
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.
 把时间戳转成日期时间:
CONVERT TIME STAMP IV_TIMESTAMP
               TIME ZONE  LV_UTCZONE
               INTO DATE  EV_DATE
                    TIME  EV_TIME.
把时间戳转成日期时 函数 
CALL FUNCTION ' CIF_GEN4_CONVERT_TIMESTAMP'
  EXPORTING
    iv_timestamp                = l_timestamp3
   IV_TIMEZONE                  = SY-ZONLO
SAP时间转换为Javashijainchuo
SAP 日期时间处理总结_第1张图片
将ABAP时间戳转换为JAVA时间戳
DATA:lv_timestampl TYPE timestampl."长时间格式的时间戳
DATA:lv_str_timestampl TYPE char22.
GET TIME STAMP FIELD lv_timestampl.
调用系统类方法进行数据获得java时间戳(毫秒级别)
DATA lv_java_timestamp TYPE string.
cl_pco_utility=>convert_abap_timestamp_to_java(
  EXPORTING
    iv_date      = conv #( lv_str_timestampl(8)    )
    iv_time      = conv #( lv_str_timestampl+8(6) )
    iv_msec      = conv #( lv_str_timestampl+15(3) )
  IMPORTING
    ev_timestamp = lv_java_timestamp  ).
        CLEAR:LV_JAVA_TIMESTAMP,LV_DATE,LV_TIME.
        LV_JAVA_TIMESTAMP =  LS_LIST-CREATETIMESTAMP.
         CL_PCO_UTILITY=>CONVERT_JAVA_TIMESTAMP_TO_ABAP(
           EXPORTING
            IV_TIMESTAMP = LV_JAVA_TIMESTAMP
          IMPORTING
            EV_DATE = LV_DATE
            EV_TIME = LV_TIME  ).
         CONVERT DATE LV_DATE TIME LV_TIME INTO TIME STAMP DATA(LV_TIMESTAMP2) TIME ZONE ' UTC'.
        CLEAR:LV_DATE,LV_TIME.
         CONVERT TIME STAMP LV_TIMESTAMP2 TIME ZONE SY-ZONLO INTO DATE LV_DATE TIME LV_TIME.
时间戳转换:
DATA TIME TYPE TIMESTAMP.
  DATA LV_TSTMP1 TYPE P.
  DATA LV_TSTMP2 TYPE P.
  DATA LV_SECS TYPE TIMESTAMP.
   CONVERT DATE P_DATE TIME P_TIME
                     INTO TIME STAMP TIME TIME ZONE SY-ZONLO.
  LV_TSTMP1 = TIME.
  LV_TSTMP2 = '19700101000000'.
  TRY.
       CALL METHOD CL_ABAP_TSTMP=>SUBTRACT
        EXPORTING
          TSTMP1 = LV_TSTMP1
          TSTMP2 = LV_TSTMP2
         RECEIVING
          R_SECS = LV_SECS.
    CATCH CX_PARAMETER_INVALID_RANGE .
    CATCH CX_PARAMETER_INVALID_TYPE .
  ENDTRY.
1、时间差计算函数(单位秒)
SWI_DURATION_DETERMINE
SAP 日期时间处理总结_第2张图片
时间戳相差秒数函数
/SDF/CMO_TIME_DIFF_GET
SAP 日期时间处理总结_第3张图片
时间分秒计算
SAP 日期时间处理总结_第4张图片
获的两个日期之间的分钟数
data min TYPE i.
CALL FUNCTION 'DELTA_TIME_DAY_HOUR'
EXPORTING
T1 = '060000'
T2 = '070000'
D1 = '20160101'
D2 = '20160101'
IMPORTING
MINUTES = min .
write min.
SAP 日期时间处理总结_第5张图片
l_timestamp =  CL_ABAP_TSTMP=>ADD(
           TSTMP   = l_timestamp  (YYMMDDHHMMSS)
           SECS    = 5 ).    【加的秒数】
日期加减年月日自建函数
SAP 日期时间处理总结_第6张图片
 获取两个日期期间的:天数、周数、月数、年数
包括期间月份的开始(月份第一天日期)、截止日期(月份最后一天日期)
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. "期间月份首尾日
两个日期相差天数月份年计算函数
FIMA_DAYS_AND_MONTHS_AND_YEARS
SAP 日期时间处理总结_第7张图片
过函数计算两个日期的差异月份
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 'RP_LAST_DAY_OF_MONTHS'
EXPORTING
day_in = '20160101'
IMPORTING
last_day_of_month = date 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 = -datum
holiday_calendar_id = 'CN'
IMPORTING
holiday_found = v_cind.
IF v_cind = ''.
CALL FUNCTION 'DAY_IN_WEEK'
EXPORTING
datum = -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 '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.

08:00:00
20:00:00
工作计划
HR_ABS_ATT_TIMES_AT_ENTRY
HR_PERSON_READ_WORK_SCHEDULE
日期格式转换
 
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
 把时间戳转成时间和日期
CALL FUNCTION 'CIF_GEN4_CONVERT_TIMESTAMP'
EXPORTING
iv_timestamp = l_timestamp3
IV_TIMEZONE = SY-ZONLO
IMPORTING
EV_DATE = l_datum3
EV_TIME = l_time3
EXCEPTIONS
TIME_CONVERSION_FAILED = 1
OTHERS = 2
.
CDS视图日期时间处理
  • 创建一个 CDS view  ZITIME ,
  1. @AbapCatalog . sqlViewName : 'ZJERRYTIME'
  2. @AbapCatalog . compiler . compareFilter : true
  3. @AbapCatalog . preserveKey : true
  4. @AccessControl . authorizationCheck : #CHECK
  5. @EndUserText . label : 'time'
  6. define view zitime as select from adr2 {
  7. key addrnumber as addnumber ,
  8. country ,
  9. valid_from ,
  10. cast ( substring ( valid_from , 1 , 8 ) as abap . dats ) as valid_from_date
  11. } where valid_from <> ''
首先使用 substring 将 valid_from 字段前8个字符提取出来,然后使用  cast 将其转换成类型  abap.dats, 因为只有类型相同的两个字段,才能进行比较。我们使用  cast 将 substring 函数返回的 char 类型的值,转换成和  $session.system_date 变量相同的类型。
转换完毕之后,将这个新的字段重命名为  valid_from_date:
SAP 日期时间处理总结_第8张图片
  1. 新建一个 CDS view ZCTIME,将前一个视图的字段  valid_from_date  同  $session.system_date  比较即可:
  1. @AbapCatalog . sqlViewName : 'ZCTIMEVIEW'
  2. @AbapCatalog . compiler . compareFilter : true
  3. @AbapCatalog . preserveKey : true
  4. @AccessControl . authorizationCheck : #CHECK
  5. @EndUserText . label : 'consumption'
  6. define view ZCTIME as select from zitime {
  7. key zitime . addnumber ,
  8. zitime . country ,
  9. zitime . valid_from_date
  10. } where valid_from_date < $session . system_date
SAP 日期时间处理总结_第9张图片
最后的运行效果:
SAP 日期时间处理总结_第10张图片
第二种方法:
CDS中的日期和时间
$session.system_date
$session.system_date            系统时间,SY-DATUM
1
TSTMP_CURRENT_UTCTIMESTAMP( )
TSTMP_CURRENT_UTCTIMESTAMP( )取得当前时间戳;
tstmp_current_utctimestamp() as Time3,//当前时间戳
1
substring(cast(tstmp_current_utctimestamp()as abap.char(17)),1,8)as Time1,//当前日期
1
substring(cast(tstmp_current_utctimestamp()as abap.char(17)),9,6)as Time2,//当前时间
1
使用需要注意时区问题
日期/时间转换的SQL函数目前只能在Core Data Service中使用,OPEN SQL不能使用。
· TSTMP_TO_DATS( tstmp, tzone, clnt, on_error )将时间戳的日期部分转为d类型
· TSTMP_TO_TIMS( tstmp, tzone, clnt, on_error )将时间戳的时间部分转换为t类型
· DATS_TIMS_TO_TSTMP( date, time, tzone, clnt, on_error )将日期和时间拼成时间戳
· ABAP_SYSTEM_TIMEZONE( clnt, on_error )取得系统时区
· ABAP_USER_TIMEZONE( user, clnt, on_error )取得用户主数据的时区
举例:
@AbapCatalog.sqlViewName: 'demo_cds_datim'
@AccessControl.authorizationCheck: #NOT_REQUIRED
define view demo_cds_date_time
  as select from
    demo_expressions
    {
      tstmp_current_utctimestamp() as tstmp,
      tstmp_to_dats( tstmp_current_utctimestamp(),
                     abap_system_timezone( $session.client,'NULL' ),
                     $session.client,
                     'NULL' )      as dat,
      tstmp_to_tims( tstmp_current_utctimestamp(),
                     abap_system_timezone( $session.client,'NULL' ),
                     $session.client,
                     'NULL' )      as tim,
      tstmp_to_dst( tstmp_current_utctimestamp(),
                    abap_system_timezone( $session.client,'NULL' ),
                    $session.client,
                    'NULL' )       as dst,
      dats_tims_to_tstmp( dats1,
                          tims1,
                          abap_system_timezone( $session.client,'NULL' ),
                          $session.client,
                         'NULL' )  as dat_tim
    }    
concat(
               cast( $session.system_date as abap.char(8) ),
               cast( tstmp_to_tims( tstmp_current_utctimestamp(), //取得时间戳
                     //abap_system_timezone( $session.client,'NULL'),//取得系统时区
                     'UTC+8',
                     $session.client,
                     'NULL')as abap.char(6) )
              ) as Time, //时间

你可能感兴趣的:(java,数学建模,servlet)