首页 文章

这个日期格式是什么? 2011-08-12T20:17:46.384Z

提问于
浏览
210

我有以下日期: 2011-08-12T20:17:46.384Z . 这是什么格式的?我试图通过 DateFormat.getDateInstance().parse(dateStr) 用Java 1.4解析它,我得到了

java.text.ParseException:Unparseable date:“2011-08-12T20:17:46.384Z”

我想我应该使用SimpleDateFormat进行解析,但我必须首先知道格式字符串 . 到目前为止,我所拥有的只是 yyyy-MM-dd ,因为我不知道 T 在这个字符串中的含义 - 与时区有关吗?此日期字符串来自Files CMIS download history media type上显示的 lcmis:downloadedOn 标记 .

5 回答

  • 298

    T只是将日期与时间分开的文字,Z表示"zero hour offset"也称为"Zulu time"(UTC) . 如果您的字符串总是有"Z",您可以使用:

    SimpleDateFormat format = new SimpleDateFormat(
        "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
    format.setTimeZone(TimeZone.getTimeZone("UTC"));
    

    或者使用Joda Time,您可以使用ISODateTimeFormat.dateTime() .

  • 23

    tl;博士

    输入字符串使用标准ISO 8601格式 .

    Instant.parse ( "2011-08-12T20:17:46.384Z" )
    

    ISO 8601

    这种格式由合理的实用标准ISO 8601定义 .

    T 将日期部分与时间部分分开 . 最后的 ZZulu 的缩写,意思是UTC .

    java.time

    与最早版本的Java捆绑在一起的旧日期时间类已被证明设计糟糕,令人困惑且麻烦 . 避免他们 .

    相反,使用Java 8及更高版本中内置的java.time框架 . java.time类取代了旧的日期时间类和非常成功的Joda-Time库 .

    在解析/生成日期时间值的文本表示时,java.time类默认使用ISO 8601 .

    Instant类表示UTC中时间轴上的一个时刻,分辨率为nanoseconds . 该类可以直接解析输入字符串,而无需定义格式化模式 .

    Instant instant = Instant.parse ( "2011-08-12T20:17:46.384Z" ) ;
    

    关于java.time

    java.time框架内置于Java 8及更高版本中 . 这些类取代了麻烦的旧legacy日期时间类,例如java.util.DateCalendarSimpleDateFormat .

    现在位于maintenance modeJoda-Time项目建议迁移到java.time类 .

    要了解更多信息,请参阅Oracle Tutorial . 并搜索Stack Overflow以获取许多示例和解释 . 规格是JSR 310 .

    从哪里获取java.time类?

    ThreeTen-Extra项目使用其他类扩展java.time . 该项目是未来可能添加到java.time的试验场 . 您可以在此处找到一些有用的类,例如IntervalYearWeekYearQuartermore .

  • -9

    不确定Java解析,但那是ISO8601:http://en.wikipedia.org/wiki/ISO_8601

  • 28

    还有其他方法可以解析它而不是第一个答案 . 要解析它:

    (1)如果要获取有关日期和时间的信息,可以将其解析为 ZonedDatetime (自 Java 8 )或 Date (旧)对象:

    // ZonedDateTime's default format requires a zone ID(like [Australia/Sydney]) in the end.
    // Here, we provide a format which can parse the string correctly.
    DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE_TIME;
    ZonedDateTime zdt = ZonedDateTime.parse("2011-08-12T20:17:46.384Z", dtf);
    

    要么

    // 'T' is a literal.
    // 'X' is ISO Zone Offset[like +01, -08]; For UTC, it is interpreted as 'Z'(Zero) literal.
    String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSX";
    
    // since no built-in format, we provides pattern directly.
    DateFormat df = new SimpleDateFormat(pattern);
    
    Date myDate = df.parse("2011-08-12T20:17:46.384Z");
    

    (2)如果您不关心日期和时间,只想将信息视为以纳秒为单位的时刻,那么您可以使用 Instant

    // The ISO format without zone ID is Instant's default.
    // There is no need to pass any format.
    Instant ins = Instant.parse("2011-08-12T20:17:46.384Z");
    
  • 1

    解决此问题的简单方法是将 "T" 替换为 " " 并删除 .384Z .

    以下是如何实现这一目标的说明:

    public static String getTime(String time) {
        if (time != null) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
            if (time.toUpperCase().contains("T") && time.toUpperCase().contains("Z")) {
                time = time.toUpperCase().replace("T", " ");
                String[] str = time.split("\\.");
    
                if (str.length != 0) {
                    return str[0];
                }
            }
    
            try {
                Date date = new Date(time);
                time = simpleDateFormat.format(date);
            } catch (Exception e) {
            //
            }
        }
        return time;
    }
    

相关问题