SAP PI UDF将没有时间的日期转换为日期时间ISO8601字符串

问题描述 投票:-1回答:1

我需要为SAP PI写一个Java函数,该函数以以下格式返回XML映射的字符串:yyyy-MM-dd T HH:mm:ss(例如2018-08-15T00:00:00),即使我的源字段只是没有时间的日期字段(例如2018-08-15)。

我已经尝试过SimpleDateFormat Java类,但无法正常工作。有没有简单的方法可以做到这一点?

在建议的帖子(答案/重复项/链接)中,我找不到想要的内容。猜猜我对问题的描述不够清楚,但是事情是我从源XML(SAP PO)获取日期,并且需要在目标XML中将其转换为ISO 8601日期。

感谢Ole,我想出了以下“入门”功能(出于完整性):

public String DH_FormatDateTimeStringB(String ndate, String npattern, Container container) throws StreamTransformationException{
//This function gets a date from the IDOC and returns it as a datetime string in ISO 8601 (ISO 8601 Representation of dates and times 
//in information interchange required. Ex: npattern = "yyyy-MM-dd")    

       DateTimeFormatter formatterDate = DateTimeFormatter.ofPattern(npattern);
       LocalDate date = LocalDate.parse(ndate, formatterDate);

       //Convert date to datetime
       LocalDateTime localDateTime1 = date.atStartOfDay(); 

       //System.out.println(localDateTime1.toString());

       return localDateTime1.toString(); 
}

由于现在只需要一个没有时间的日期,所以'StartOfDay'可能会做。也许以后再调整一下,看看字符串中是否有时间部分。

感谢所有人的帮助!

java sap iso8601 sap-xi sap-pi
1个回答
0
投票
    String dateStringFromSapPo = "2018-08-15";
    LocalDate date = LocalDate.parse(dateStringFromSapPo);
    LocalDateTime dateTime = date.atStartOfDay();
    String dateTimeStringForSapPi = dateTime.toString();
    System.out.println("String for SAP PI: " + dateTimeStringForSapPi);

此打印:

SAP PI的字符串:2018-08-15T00:00

还没有几秒钟,但是符合ISO 8601标准,因此应该可以在您的XML和SAP中使用。如果不是,则需要使用显式格式化程序:

    DateTimeFormatter dateTimeFormatter
            = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss");
    String dateTimeStringForSapPi = dateTime.format(dateTimeFormatter);

现在秒数也出来了:

SAP PI的字符串:2018-08-15T00:00:00

顺便说一句,给您一个没有时区或偏移量的日期时间字符串,让我有些担心。这不是一个时间点,将其解释为一个时间点,SAP必须假定一个时区。仅当您确定选择了预期的产品时,您才可以。如果没有,对不起,我无法告诉您解决方案。

Link: Oracle tutorial: Date Time说明如何使用java.time

© www.soinside.com 2019 - 2024. All rights reserved.