Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In this quick tutorial, we’ll explain how to convert String to Instant with Java using classes from java.time package. First, we’ll implement a solution using the LocalDateTime class. Then, we’ll use the Instant class to get an instant within a time zone.

2. Using the LocalDateTime Class

java.time.LocalDateTime represents dates and/or times without a time zone. It’s a local time object in the sense that it is valid only in a particular context and cannot be used outside this context. This context is generally the machine on which the code is executed.

To get a time from a String, we can use DateTimeFormatter to create a formatted object and pass this formatter to LocalDateTime‘s parse method. Also, we can define our own formatter or use predefined formatters provided by the DateTimeFormatter class.

Let’s see how to use LocalDateTime.parse() to get a time from a String:

String stringDate = "09:15:30 PM, Sun 10/09/2022"; 
String pattern = "hh:mm:ss a, EEE M/d/uuuu"; 
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern, Locale.US); 
LocalDateTime localDateTime = LocalDateTime.parse(stringDate, dateTimeFormatter);

In the above example, we use the LocalDateTime class, which is the standard class for representing a date with a time, to parse the date String. We can also use java.time.LocalDate to represent only a date with no time.

3. Using the Instant Class

The java.time.Instant class, one of the main classes of the Date-Time API, encapsulates a point on the timeline. Also, it’s similar to the java.util.Date class but gives nanosecond precision.

In our next example, we’ll use the previous LocalDateTime to get an instant with an assigned ZoneId:

String stringDate = "09:15:30 PM, Sun 10/09/2022"; 
String pattern = "hh:mm:ss a, EEE M/d/uuuu"; 
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern, Locale.US); 
LocalDateTime localDateTime = LocalDateTime.parse(stringDate, dateTimeFormatter); 
ZoneId zoneId = ZoneId.of("America/Chicago"); 
ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId); 
Instant instant = zonedDateTime.toInstant();

In the above example, we first create a ZoneId object, which is used to identify a time zone, and then, we provide the conversion rules between LocalDateTime and Instant.

Next, we use ZonedDateTime, which encapsulates a date and time with a time zone and its corresponding offset. The ZonedDateTime class is the closest class in the Date-Time API to the java.util.GregorianCalendar class. Finally, we get an Instant using the ZonedDateTime.toInstant() method, which adjusts a moment from a time zone to UTC.

4. Conclusion

In this quick tutorial, we explained how to convert a String to an Instant with Java using classes from java.time package. As always, the code snippets are available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.