Convert a date string to an Instant in Java
This post will discuss how to convert a date string to an Instant in Java.
Instant in Java was introduced in Java 8 as a part of the new Date and Time API. Instant is a class that represents a specific moment on the time-line, measured in nanoseconds from the epoch of 1970-01-01T00:00:00Z. It was introduced was to overcome the limitations and drawbacks of the old java.util.Date and java.util.Calendar classes, which were mutable, not thread-safe, and confusing to use. Instant, on the other hand, is immutable, thread-safe, and easy to use. It is one of the core classes of the Date and Time API, along with LocalDate, LocalTime, LocalDateTime, ZonedDateTime, OffsetDateTime, and Duration. These classes were inspired by the Joda-Time library and provide a comprehensive and consistent model for working with date and time values in Java.
1. Using Instant.parse() method
To convert a date string to an instant in Java, we can use the Instant.parse() method, which parses the string using the ISO-8601 format. This format is widely used for date and time representations and has the following syntax: YYYY-MM-DDTHH:mm:ss.SSSZ, where T separates the date and time, and Z is the time zone offset from UTC. If a non-standard date string is passed to the Instant.parse() method, it cannot be parsed and DateTimeParseException is thrown. For example, 2020-04-06T11:54:03+09:00 is an ISO-8601 formatted string that represents the date and time in Tokyo, Japan. It can be parsed using the Instant.parse() method, as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.time.Instant; class Main { public static void main(String[] args) { // A string date in ISO 8601 format String dateString = "2020-04-10T10:10:05+09:00"; // Parse the string to an Instant object Instant instant = Instant.parse(dateString); // Output: 2020-04-10T01:10:05Z System.out.println(instant.toString()); } } |
2. Using toInstant() method
If the date string is not in the ISO-8601 format, we can use a DateTimeFormatter to specify a custom pattern for parsing the string. Then, we can use the LocalDateTime.parse() method to parse the string into a LocalDateTime object, which represents a date and time without a time zone. Finally, we can use the atZone() method to apply a time zone to the LocalDateTime object and get a ZonedDateTime object, which represents a date and time with a time zone. Then, we can use the toInstant() method to convert the ZonedDateTime object into an Instant date. The Instant object represents a point on the timeline in UTC. We can use the toString() method to get a string representation of the instant in the ISO-8601 format. To illustrate, consider the following example.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.Locale; class Main { public static void main(String[] args) { // A date string in a custom format String dateString = "02:30 PM, 4/10/2020"; // Create a DateTimeFormatter object with the custom format DateTimeFormatter formatter = DateTimeFormatter .ofPattern("hh:mm a, M/d/yyyy", Locale.US); // Parse the date string to a `LocalDateTime` object LocalDateTime localDateTime = LocalDateTime.parse(dateString, formatter); // Set the time zone to America/Toronto ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("America/Toronto")); // Convert the Date object to an Instant object Instant instant = zonedDateTime.toInstant(); // Output: 2020-04-10T18:30:00Z System.out.println(instant.toString()); } } |
That’s all about converting a date string to an Instant in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)