简体   繁体   English

Android - 如何解析此日期[2014-04-23T14:20:00.000-07:00]?

[英]Android - How to parse this date [ 2014-04-23T14:20:00.000-07:00 ]?

I want to convert this date format : 我想转换这种日期格式:

2014-04-23T14:20:00.000-07:00

into a date object so I can use this method to get the difference : 到一个日期对象,所以我可以使用此方法来获得差异:

DateUtils.getDateDifference(DATE_OBJECT);

... ...

I've tried using 'SimpleDateFormat' but it throws an exception, checked the code many times, but doesn't work: 我尝试使用'SimpleDateFormat',但它抛出异常,多次检查代码,但不起作用:

    String pubDate = "2014-04-23T14:20:00.000-07:00";
    SimpleDateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss Z", Locale.ENGLISH);
    try {
        Date pDate =  df.parse(pubDate);
        pubDate = "This post was published " + DateUtils.getDateDifference(pDate);
    } catch (ParseException e) {
        Log.e("DATE PARSING", "Error parsing date..");
        pubDate = "Couldn't be retrieved.";
    } 

What is the simplest way to achieve it successfully? 成功实现它的最简单方法是什么?

According to Android documentation (link given in comment of @MarcoAcierno) you need the symbol Z five times to parse the timezone offset. 根据Android文档(@MarcoAcierno评论中给出的链接),您需要符号Z五次来解析时区偏移量。 Note that in Java-6 there is no built-in solution for handling the colon in offset part, but Java-7 has introduced the new symbol X (here three times: XXX). 请注意,在Java-6中,没有用于处理偏移部分中冒号的内置解决方案,但Java-7引入了新符号X(此处三次:XXX)。 Another example for (Android != Java). (Android!= Java)的另一个例子。 So the final pattern looks like: 所以最终的模式如下:

SimpleDateFormat sdf =
  new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ");
java.util.Date d = sdf.parse("2014-04-23T14:20:00.000-07:00");

Specifying a Locale is not necessary because your format does not contain any language- or locale-sensitive parts (is just pure ISO-format). 不需要指定区域设置,因为您的格式不包含任何语言或区域敏感部分(仅为纯ISO格式)。

If you used the excellent Joda-Time library instead of the notoriously troublesome java.util.Date and SimpleTextFormat classes,,you could pass that standard ISO 8601 String directly to a DateTime constructor. 如果您使用了优秀的Joda-Time库而不是臭名昭着的java.util.Date和SimpleTextFormat类,则可以将该标准ISO 8601字符串直接传递给DateTime构造函数。 Joda-Time uses ISO 8601 for its defaults. Joda-Time使用ISO 8601作为默认值。

DateTime dateTime = new DateTime( pubDate );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从字符串“ 2016-09-14T00:00:00.000-07:00 / 2016-09-15T00:00:00.000-07:00”确定时区 - Determine timezone from string “2016-09-14T00:00:00.000-07:00/2016-09-15T00:00:00.000-07:00” 如何将“2020-12-20T00:00:00.000Z”转换为 java.util.Date? - How to convert “2020-12-20T00:00:00.000Z” to java.util.Date? 无法解释的日期:“2014年10月10日星期二23:11:07”(抵消20日) - Unparseable date: “Fri Oct 10 23:11:07 IST 2014” (at offset 20) 将字符串 ("2022-12-23T07:20:00") 时间转换为 ZonedDateTime - convert string ("2022-12-23T07:20:00" ) time into ZonedDateTime 解析日期字符串,格式为[2012-07-15T20:55:33 + 00:00] - Parsing date string in Java of format [2012-07-15T20:55:33+00:00] 如何用Java将“ 2019-08-07T14:00:00-0400”转换为SQL DATETIME格式? - How to convert “2019-08-07T14:00:00-0400” to SQL DATETIME format in Java? DateTime("2019-08-07T19:20-5:00") 转换为具有偏移值的本地日期时间 - DateTime("2019-08-07T19:20-5:00") conversion to local date time with offset value java 日期格式将 'M/d/yyyy' 转换为 'yyyy-MM-dd'T'HH:mm:ss.SSSXXX' 就像 2021-04-05T00:00-07:00[UTC-07:00] - java Date format convert 'M/d/yyyy' to 'yyyy-MM-dd'T'HH:mm:ss.SSSXXX' like 2021-04-05T00:00-07:00[UTC-07:00] 将类型为“ 2015-23-07T00:00:00Z”的字符串转换为格式为“ 07/23 / 2015T00:00:00Z”的XMLGregorianCalender - Convert String of type “2015-23-07T00:00:00Z” to XMLGregorianCalender of format “07/23/2015T00:00:00Z” 从另一个java.util.Date对象获取java.util.Date对象为(10/7/14)(IST 2014年10月7日11:21:00) - Getting java.util.Date Object As (10/7/14 ) From Another java.util.Date Object (Tue Oct 07 11:21:00 IST 2014)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM