响应中的日期格式 [英] date formatting from response

查看:154
本文介绍了响应中的日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

调用 OData服务并获得日期的 ATOM XML 响应列为我提供的日期值为

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>

但是。目前,我有一个日期值,例如 2012年8月2日星期四02:00:00 GMT + 0200(MitteleuropäischeSommerzeit) 。我想将此值转换为Edm.DateTIme格式,如上所示。

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.

推荐答案

以下内容:

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

打印出:

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

请注意转换时区。 Java SimpleDateFormat 期望在偏移量中使用冒号。

Note the conversion for the time zone. The Java SimpleDateFormat expects a colon in the offset.

说到时区,我不知道OData Atom XML在某处指定时区。 Edm.DataTime没有此类功能。

Speaking of time zones, I don't know if the OData Atom XML somewhere specifies a time zone. Edm.DataTime has no such feature.

编辑:如果要将输出转换为特定时区(我的默认TZ是GMT +1,因此它为输入02:00 GMT + 2打印03:00),您可以设置outFormat的时区,例如:

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

这篇关于响应中的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆