date formatting from response

你。 提交于 2019-12-02 10:19:23

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"));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!