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

问题描述 投票:260回答:6

我有以下日期: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在这个字符串中意味着什么 - 与时区相关的东西?此日期字符串来自lcmis:downloadedOn上显示的Files CMIS download history media type标记。

java date time format simpledateformat
6个回答
384
投票

T只是将日期与时间分开的文字,Z表示“零小时偏移”,也称为“祖鲁时间”(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()


42
投票

tl;dr

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

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

ISO 8601

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

T将日期部分与时间部分分开。最后的Z意味着UTC(即,从零小时 - 分钟 - 秒的UTC偏移)。 Zpronounced “Zulu”

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

About java.time

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

现在在Joda-Timemaintenance mode项目建议迁移到java.time班。

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

从哪里获取java.time类?

ThreeTen-Extra项目使用其他类扩展了java.time。该项目是未来可能添加到java.time的试验场。你可能会在这里找到一些有用的类,如IntervalYearWeekYearQuartermore


24
投票

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


4
投票

还有其他方法可以解析它而不是第一个答案。要解析它: (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");

0
投票

您可以使用以下示例。

    String date = "2011-08-12T20:17:46.384Z";

    String inputPattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";

    String outputPattern = "yyyy-MM-dd HH:mm:ss";

    LocalDateTime inputDate = null;
    String outputDate = null;


    DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern(inputPattern, Locale.ENGLISH);
    DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern(outputPattern, Locale.ENGLISH);

    inputDate = LocalDateTime.parse(date, inputFormatter);
    outputDate = outputFormatter.format(inputDate);

    System.out.println("inputDate: " + inputDate);
    System.out.println("outputDate: " + outputDate);

0
投票

此技术将java.util.Date转换为UTC格式(或任何其他格式)并再次返回。

像这样定义一个类:

import java.util.Date;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class UtcUtility {

public static DateTimeFormatter UTC = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").withZoneUTC();


public static Date parse(DateTimeFormatter dateTimeFormatter, String date) {
    return dateTimeFormatter.parseDateTime(date).toDate();
}

public static String format(DateTimeFormatter dateTimeFormatter, Date date) {
    return format(dateTimeFormatter, date.getTime());
}

private static String format(DateTimeFormatter dateTimeFormatter, long timeInMillis) {
    DateTime dateTime = new DateTime(timeInMillis);
    String formattedString = dateTimeFormatter.print(dateTime);
    return formattedString;
}

}

然后像这样使用它:

Date date = format(UTC, "2020-04-19T00:30:07.000Z")

要么

String date = parse(UTC, new Date())

如果需要,您还可以定义其他日期格式(不仅仅是UTC)

© www.soinside.com 2019 - 2024. All rights reserved.