Calling a OData service
and getting the ATOM XML
response of a date
column gives me date value as
<d:BUSINESS_DATE m:type="Edm.DateTime">2012-08-02T00:00:00.0000000</d:BUSINESS_DATE>
But. currently I have a date value like "Thu Aug 02 2012 02:00:00 GMT+0200 (Mitteleuropäische Sommerzeit)"
. I would want to conver this value to Edm.DateTIme format as shown above.
Any functions to achieve the same. Any workaroud. Please help.
The 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));
}
prints out:
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
Note the conversion for the time zone. The Java SimpleDateFormat
expects a colon in the offset.
Speaking of time zones, I don't know if the OData Atom XML somewhere specifies a time zone. Edm.DataTime has no such feature.
Edit: If you want to convert the output to a specific time zone (my default TZ is GMT+1, so it prints 03:00 for the input 02:00 GMT+2), you can set the time zone for the outFormat, e.g.:
outFormat.setTimeZone(TimeZone.getTimeZone("PST"));
来源:https://stackoverflow.com/questions/22247016/date-formatting-from-response