[英]SAP PI UDF to convert date without time to datetime ISO8601 string
I need to write a java function for SAP PI which returns a string for my XML mapping in the format: yyyy-MM-dd T HH:mm:ss (eg, 2018-08-15T00:00:00
) even when my source field is just a date field without time (eg, 2018-08-15
).我需要为 SAP PI 编写一个 java 函数,它以以下格式为我的 XML 映射返回一个字符串:yyyy-MM-dd T HH:mm:ss(例如, 2018-08-15T00:00:00
)即使我的源field 只是一个没有时间的日期字段(例如, 2018-08-15
)。
I've tried the SimpleDateFormat
Java class but I can't get it to work.我已经尝试过SimpleDateFormat
Java 类,但我无法让它工作。 Is there a simple way of doing this?有没有一种简单的方法可以做到这一点?
In the suggested posts (answers / duplicates / links) I couldn't find what I was looking for.在建议的帖子(答案/重复/链接)中,我找不到我要找的东西。 Guess I didn't make myself clear enough describing the problem but the thing was I'm getting the date from a source XML (SAP PO) and I need to convert it to an ISO 8601 date in the target XML.猜猜我没有让自己足够清楚地描述问题,但问题是我从源 XML (SAP PO) 获取日期,我需要将其转换为目标 XML 中的 ISO 8601 日期。
Thanks to Ole I came up with the following 'beginners' function (for completeness):感谢 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();
}
Since it now only takes a date without time it might the 'StartOfDay' will do.由于它现在只需要一个没有时间的日期,因此“StartOfDay”可能会这样做。 Maybe I adjust it later on to see if there's a time part in the string.也许我稍后会调整它以查看字符串中是否有时间部分。
Thnx all for helping out!谢谢大家的帮助!
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);
This prints:这打印:
String for SAP PI: 2018-08-15T00:00 SAP PI 的字符串:2018-08-15T00:00
It hasn't got seconds, but conforms with the ISO 8601 standard, so should work in your XML and SAP.它没有几秒钟,但符合 ISO 8601 标准,所以应该在您的 XML 和 SAP 中工作。 If it doesn't, you will need to use an explicit formatter:如果没有,您将需要使用显式格式化程序:
DateTimeFormatter dateTimeFormatter
= DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss");
String dateTimeStringForSapPi = dateTime.format(dateTimeFormatter);
Now the seconds come out too:现在秒数也出来了:
String for SAP PI: 2018-08-15T00:00:00 SAP PI 的字符串:2018-08-15T00:00:00
As an aside, it worries me a bit to give you a date-time string without time zone or offset.顺便说一句,给你一个没有时区或偏移量的日期时间字符串让我有点担心。 It's not a point in time and to interpret it as one, SAP will have to assume a time zone.这不是一个时间点,要将其解释为一个时间点,SAP 必须假设一个时区。 Only if you are sure it picks the intended one, you're fine.只有当您确定它选择了预期的那个时,您才可以。 If not, I'm sorry that I cannot tell you the solution.如果没有,很抱歉我不能告诉你解决方案。
Link: Oracle tutorial: Date Time explaining how to use java.time
链接: Oracle 教程:解释如何使用java.time
日期时间
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.