简体   繁体   中英

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 ).

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 it might the 'StartOfDay' will do. 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

It hasn't got seconds, but conforms with the ISO 8601 standard, so should work in your XML and 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

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM