本文翻译自:What is this date format? 2011-08-12T20:17:46.384Z
I have the following date: 2011-08-12T20:17:46.384Z
. 我有以下日期: 2011-08-12T20:17:46.384Z
。 What format is this? 这是什么格式? I'm trying to parse it with Java 1.4 via DateFormat.getDateInstance().parse(dateStr)
and I'm getting 我试图通过DateFormat.getDateInstance().parse(dateStr)
用Java 1.4解析它,
java.text.ParseException: Unparseable date: "2011-08-12T20:17:46.384Z" java.text.ParseException:无法解析的日期:“ 2011-08-12T20:17:46.384Z”
I think I should be using SimpleDateFormat for parsing, but I have to know the format string first. 我想我应该使用SimpleDateFormat进行解析,但是我必须首先知道格式字符串。 All I have for that so far is yyyy-MM-dd
, because I don't know what the T
means in this string--something time zone-related? 到目前为止,我所拥有的只是yyyy-MM-dd
,因为我不知道T
在此字符串中的含义是什么-与时区有关? This date string is coming from the lcmis:downloadedOn
tag shown on Files CMIS download history media type . 此日期字符串来自文件CMIS下载历史记录媒体类型上显示的lcmis:downloadedOn
标记。
#1楼
参考:https://stackoom.com/question/ZGXv/这个日期格式是什么-T-Z
#2楼
tl;dr tl; dr
Standard ISO 8601 format is used by your input string. 输入字符串使用标准ISO 8601格式。
Instant.parse ( "2011-08-12T20:17:46.384Z" )
ISO 8601 ISO 8601
This format is defined by the sensible practical standard, ISO 8601 . 此格式由明智的实用标准ISO 8601定义 。
The T
separates the date portion from the time-of-day portion. T
将日期部分与时间部分分开。 The Z
on the end means UTC (that is, an offset-from-UTC of zero hours-minutes-seconds). 末尾的Z
表示UTC (即,相对于UTC的偏移量为零小时-分钟-秒)。 The Z
is pronounced “Zulu” . Z
发音为“ Zulu” 。
java.time java.time
The old date-time classes bundled with the earliest versions of Java have proven to be poorly designed, confusing, and troublesome. 事实证明,与最早的Java版本捆绑在一起的旧的日期时间类设计不佳,令人困惑且麻烦。 Avoid them. 避免他们。
Instead, use the java.time framework built into Java 8 and later. 相反,请使用Java 8及更高版本中内置的java.time框架。 The java.time classes supplant both the old date-time classes and the highly successful Joda-Time library. java.time类取代了旧的日期时间类和非常成功的Joda-Time库。
The java.time classes use ISO 8601 by default when parsing/generating textual representations of date-time values. 解析/生成日期时间值的文本表示时,java.time类默认使用ISO 8601 。
The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds . Instant
类表示UTC时间线上的时刻,分辨率为纳秒 。 That class can directly parse your input string without bothering to define a formatting pattern. 该类可以直接解析您的输入字符串,而无需费心定义格式设置模式。
Instant instant = Instant.parse ( "2011-08-12T20:17:46.384Z" ) ;
About java.time 关于java.time
The java.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
. 这些类取代了麻烦的旧的旧式日期时间类,例如java.util.Date
, Calendar
和SimpleDateFormat
。
The Joda-Time project, now in maintenance mode , advises migration to the java.time classes. 现在处于维护模式的Joda-Time项目建议迁移到java.time类。
To learn more, see the Oracle Tutorial . 要了解更多信息,请参见Oracle教程 。 And search Stack Overflow for many examples and explanations. 并在Stack Overflow中搜索许多示例和说明。 Specification is JSR 310 . 规格为JSR 310 。
Where to obtain the java.time classes? 在哪里获取java.time类?
- Java SE 8 , Java SE 9 , and later Java SE 8 , Java SE 9和更高版本
- Built-in. 内置的
- Part of the standard Java API with a bundled implementation. 标准Java API的一部分,具有捆绑的实现。
- Java 9 adds some minor features and fixes. Java 9添加了一些次要功能和修复。
- Java SE 6 and Java SE 7 Java SE 6和Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport . java.time的许多功能在ThreeTen- Backport中都被反向移植到Java 6和7。
- Android 安卓系统
- Later versions of Android bundle implementations of the java.time classes. 更高版本的Android捆绑了java.time类的实现。
- For earlier Android, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). 对于较早的Android, ThreeTenABP项目改编了ThreeTen-Backport (如上所述)。 See How to use ThreeTenABP… . 请参阅如何使用ThreeTenABP… 。
The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目使用其他类扩展了java.time。 This project is a proving ground for possible future additions to java.time. 该项目为将来可能在java.time中添加内容提供了一个试验场。 You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more . 您可以在这里找到一些有用的类,比如Interval
, YearWeek
, YearQuarter
,和更多 。
#3楼
There are other ways to parse it rather than the first answer. 还有其他解析方法,而不是第一个答案。 To parse it: 要解析它:
(1) If you want to grab information about date and time, you can parse it to a ZonedDatetime
(since Java 8 ) or Date
(old) object: (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);
or 要么
// '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) If you don't care the date and time and just want to treat the information as a moment in nanoseconds, then you can use Instant
: (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");
#4楼
You can use the following example. 您可以使用以下示例。
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);
#5楼
This technique translates java.util.Date to UTC format (or any other) and back again. 此技术将java.util.Date转换为UTC格式(或任何其他格式),然后再次返回。
Define a class like so: 像这样定义一个类:
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;
}
} }
Then use it like this: 然后像这样使用它:
Date date = format(UTC, "2020-04-19T00:30:07.000Z")
or 要么
String date = parse(UTC, new Date())
You can also define other date formats if you require (not just UTC) 如果需要,您还可以定义其他日期格式(不仅限于UTC)
#6楼
@John-Skeet gave me the clue to fix my own issue around this. @ John-Skeet为我提供了解决此问题的线索。 As a younger programmer this small issue is easy to miss and hard to diagnose. 作为一个年轻的程序员,这个小问题很容易遗漏并且很难诊断。 So Im sharing it in the hopes it will help someone. 因此,我希望分享它,希望对您有所帮助。
My issue was that I wanted to parse the following string contraining a time stamp from a JSON I have no influence over and put it in more useful variables. 我的问题是我想解析下面的字符串,与来自我没有影响的JSON的时间戳形成鲜明对比,并将其放入更有用的变量中。 But I kept getting errors. 但是我不断出错。
So given the following (pay attention to the string parameter inside ofPattern(); 因此,给出以下内容(注意Pattern()内的字符串参数;
String str = "20190927T182730.000Z"
LocalDateTime fin;
fin = LocalDateTime.parse( str, DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss.SSSZ") );
Error: 错误:
Exception in thread "main" java.time.format.DateTimeParseException: Text
'20190927T182730.000Z' could not be parsed at index 19
The problem? 问题? The Z at the end of the Pattern needs to be wrapped in 'Z' just like the 'T' is. 就像“ T”一样,模式末尾的Z需要用“ Z”包裹。 Change "yyyyMMdd'T'HHmmss.SSSZ"
to "yyyyMMdd'T'HHmmss.SSS'Z'"
and it works. 将"yyyyMMdd'T'HHmmss.SSSZ"
更改为"yyyyMMdd'T'HHmmss.SSS'Z'"
。
Removing the Z from the pattern alltogether also led to errors. 从样式中完全删除Z也会导致错误。
Frankly, I'd expect a Java class to have anticipated this. 坦白地说,我希望Java类已经预见到这一点。