-1

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!

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Mike Dole
  • 675
  • 2
  • 14
  • 30
  • 2
    No you don't. You need to find a good date time library, and use that. Don't ever roll your own: you have no idea how insane dates get. Not even after learning how insane dates get. – Mike 'Pomax' Kamermans Jul 03 '18 at 18:40
  • 2
    Yes, `DatetimeFormat.ISO_FORMAT.format(yourDatetime) ` – daniu Jul 03 '18 at 18:41
  • 2
    Don’t use `SimpleDateFormat`, it’s not only long outdated, it’s also notoriously troublesome. Instead use [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). BTW you have forgot to show us your attempt and how it failed. – Ole V.V. Jul 04 '18 at 05:15
  • 2
    Search for the java.time class `LocalDateTime`. – Basil Bourque Jul 04 '18 at 07:47
  • 1
    Assuming `yourDate` is a `LocalDate`, use `yourDate.atStartOfDay().toString()`. It gives a string like `2018-11-29T00: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 `yourDate.atStartOfDay().format(DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss"))`. – Ole V.V. Jul 04 '18 at 10:05
  • Thanks Ole, that is exactly what I was looking for! – Mike Dole Jul 04 '18 at 15:13
  • Related questions (previously marked as originals of this one): [Convert java.util.Date to String](https://stackoverflow.com/questions/5683728/convert-java-util-date-to-string) *15 answers*. [How to get current moment in ISO 8601 format with date, hour, and minute?](https://stackoverflow.com/questions/3914404/how-to-get-current-moment-in-iso-8601-format-with-date-hour-and-minute) *21 answers*. – Ole V.V. Jul 05 '18 at 09:38
  • I have unmarked this questions as duplicate because none of the linked originals really gave the precise information the asker was after, and I didn’t find it appropriate to add it as an answer to any of those. Mike Dole, if you like, you may add your solution as an answer here rather than giving it in the question (where it doesn’t really belong). – Ole V.V. Jul 05 '18 at 09:38

1 Answers1

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

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

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161