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
(e.g., 2018-08-15T00:00:00
) even when my source field is just a date field without time (e.g., 2018-08-15
).
I've tried the SimpleDateFormat
Java class but I can't get it to work.
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.
Thanks to Ole I came up with the following 'beginners' function (for completeness):
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, maybe atStartOfDay
will do. Maybe I adjust it later on to see if there's a time part in the string.
Thanks all for helping out!