本文翻譯自: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類已經預見到這一點。