java.time
The date-time API of java.util
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.
Using the modern date-time API:
Since your date-time string conforms to ISO-8601 format, you can parse it directly (i.e. without using a DateTimeFormatter
) to OffsetDateTime
.
import java.time.OffsetDateTime;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) {
String strDateTime = "2020-12-16T16:27:57+00:00";
OffsetDateTime odt = OffsetDateTime.parse(strDateTime);
System.out.println(odt);
System.out.println("No of milliseconds from Epoch: " + odt.toInstant().toEpochMilli());
// Alternatively
System.out.println("No of milliseconds from Epoch: " + TimeUnit.SECONDS.toMillis(odt.toEpochSecond()));
}
}
Output:
2020-12-16T16:27:57Z
No of milliseconds from Epoch: 1608136077000
No of milliseconds from Epoch: 1608136077000
What went wrong with your answer?
- Use of
ZZ:ZZ
instead of a single X
: Check the documentation to learn more about it. Also, I am not sure whether you have enclosed ZZ:ZZ
within quotes, used for formatting code, just to highlight it as a code or you have really done this in actual code. Make sure you never enclose Z
within single quotes; otherwise, it will simply mean Z
as a character literal instead of timezone. Check this answer to understand it better.
- Although it's not mandatory for this parsing, you should never use
SimpleDateFormat
or DateTimeFormatter
without a Locale
. Check this answer to understand it better.
Using legacy API:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Date;
import java.util.Locale;
public class Main {
public static void main(String[] args) throws ParseException {
String strDateTime = "2020-12-16T16:27:57+00:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX", Locale.ENGLISH);
Date date = sdf.parse(strDateTime);
System.out.println(date);
System.out.println("No of milliseconds from Epoch: " + date.getTime());
// Given below is how you can convert the legacy type, java.util.Date to the
// modern type java.time.Instant
Instant instant = date.toInstant();
System.out.println("No of milliseconds from Epoch: " + instant.toEpochMilli());
}
}
Output:
Wed Dec 16 16:27:57 GMT 2020
No of milliseconds from Epoch: 1608136077000
No of milliseconds from Epoch: 1608136077000
Note that the 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). When you print an object of java.util.Date
, its toString
method returns the date-time in the JVM's timezone, calculated from this milliseconds value. If you need to print the date-time in a different timezone, you will need to set the timezone to SimpleDateFormat
and obtain the formatted string from it.