Formatting date from response

Calling OData serviceand receiving a ATOM XMLcolumn response dategives me a date value as

<d:BUSINESS_DATE m:type="Edm.DateTime">2012-08-02T00:00:00.0000000</d:BUSINESS_DATE>

But. I currently have a date value, for example "Thu Aug 02 2012 02:00:00 GMT+0200 (Mitteleuropäische Sommerzeit)". I would like to convert this value to Edm.DateTIme format as shown above.

Any functions to achieve the same. Any worker. Please, help.

0
source share
1 answer

Following:

public static void main(String[] args) {
    String fromDate = "Thu Aug 02 2012 02:00:00 GMT+0200 (Mitteleuropäische Sommerzeit)";
    String fromDateConverted = fromDate.replaceAll("\\+(..)(..)", "+$1:$2");
    System.out.println("ORG: " + fromDate);
    System.out.println("CNV: " + fromDateConverted);
    SimpleDateFormat parseFormat = new SimpleDateFormat("EE MMM dd yyyy HH:mm:ss zzzz", Locale.ENGLISH);

    Date theDate = parseFormat.parse(fromDateConverted);

    // OData Edm.DateTime:
    // yyyy "-" mm "-" dd "T" hh ":" mm [":" ss["." fffffff]]
    SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.0000000");

    System.out.println("EDM: " + outFormat.format(theDate));
}

produces:

ORG: Thu Aug 02 2012 02:00:00 GMT+0200 (Mitteleuropäische Sommerzeit)
CNV: Thu Aug 02 2012 02:00:00 GMT+02:00 (Mitteleuropäische Sommerzeit)
EDM: 2012-08-02T03:00:00.0000000

Pay attention to the conversion for the time zone. Java SimpleDateFormatexpects colons in offset.

, , XML OData Atom XML . Edm.DataTime .

. ( TZ - GMT + 1, 03:00 02:00 GMT + 2), outFormat, :

outFormat.setTimeZone(TimeZone.getTimeZone("PST"));
+2

Source: https://habr.com/ru/post/1721864/


All Articles