java.time
You do not need DateTimeFormatter
to parse the given date-time string.
Java SE 8 Date-Time API(java.time
API or the modern Date-Time API) is based on ISO 8601 and does not require using a DateTimeFormatter
object explicitly as long as the Date-Time string conforms to the ISO 8601 standards.
The Z
in the string is the timezone designator for zero-timezone offset. It stands for Zulu and specifies the Etc/UTC
timezone (which has the timezone offset of +00:00
hours).
The T
in the string is just the Date-Time separator as per the ISO-8601 standards.
Demo:
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
public class Main {
public static void main(String[] args) {
String strDateTime = "2011-08-12T20:17:46.384Z";
Instant instant = Instant.parse(strDateTime);
OffsetDateTime odt = OffsetDateTime.parse(strDateTime);
ZonedDateTime zdt = ZonedDateTime.parse(strDateTime);
System.out.println(instant);
System.out.println(odt);
System.out.println(zdt);
}
}
Output:
2011-08-12T20:17:46.384Z
2011-08-12T20:17:46.384Z
2011-08-12T20:17:46.384Z
Learn more about java.time
, the modern Date-Time API* from Trail: Date Time.
The legacy Date-time API
The legacy Date-time API (java.util
Date-Time API and their formatting API, SimpleDateFormat
) are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.
For the sake of completeness, I've written a solution to parse this Date-Time string using the legacy API.
Do not use 'Z'
in the pattern with the Date-Time parsing/formatting API.
As already described above, Z
(without quotes) is the timezone designator for zero-timezone offset whereas 'Z'
is just a character literal and it does not hold any meaning. Use the format, y-M-d'T'H:m:s.SSSXXX
. Check the documentation to learn more about these symbols.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class Main {
public static void main(String[] args) throws ParseException {
String strDateTime = "2011-08-12T20:17:46.384Z";
SimpleDateFormat sdf = new SimpleDateFormat("y-M-d'T'H:m:s.SSSXXX", Locale.ENGLISH);
Date date = sdf.parse(strDateTime);
// ...
}
}
Note that a java.util.Date
object is not a real Date-Time object like the modern Date-Time types; rather, it represents the number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT
(or UTC). Since it does not hold any format and timezone information, it applies the format, EEE MMM dd HH:mm:ss z yyyy
and the JVM's timezone to return the value of Date#toString
derived from this milliseconds value. If you need to print the Date-Time in a different format and timezone, you will need to use a SimpleDateFormat
with the desired format and the applicable timezone e.g.
sdf.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
String formatted = sdf.format(date);
System.out.println(formatted); // 2011-8-12T20:17:46.384Z
Joda Date-Time API
Quoted below is a notice at the Home Page of Joda-Time:
Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.
Again, for the sake of completeness, I've written a solution to parse this Date-Time string using the Joda Date-Time API.
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
String dateTimeStr = "2011-08-12T20:17:46.384Z";
DateTimeFormatter dtf = DateTimeFormat.forPattern("y-M-d'T'H:m:s.SSSZ").withOffsetParsed();
DateTime dateTime = dtf.parseDateTime(dateTimeStr);
System.out.println(dateTime);
}
}
Output:
2011-08-12T20:17:46.384Z
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
Z
at the end is short forZulu
and means UTC. This format most certainly is part of the ISO 8601 collection of standard date-time text formats. By the way, these standard formats are used by default in the java.time classes. – Appellatejava.util.Date
,java.util.Calendar
, andjava.text.SimpleDateFormat
are now legacy, supplanted by the java.time classes built into Java 8 & Java 9. See Tutorial by Oracle. – Appellate