PostEazy

Inspired By Digital World

1. Overview

In this article we’ll explain how to format an instant into the form of a string in Java.

We’ll begin with some background information about what is an instant in Java. We’ll then show the way to address our main query using the core Java along with a third-party library, such as Joda-Time.

Read More About core java tutorial for beginners

2. Instantly Formatting With Core Java Core Java

A measured timestamp from the Java epoch of 1970-01-01T00:00:00Z, according to the Java documentation.

Java 8 comes with a useful class known as Instant to represent an instantaneous time within the timeline. In general, we can utilize this class to store timestamps for events within our apps.

We now are aware of the definition of an instant in Java. Let’s take a look at how we can transform it into a String object.

A) Utilizing the DateTimeFormatter class

The general rule is that we require a formatter to format Instant objects. It is good news for Java users that Java 8 introduced the DateTimeFormatter class that can uniformly format the dates as well as times.

In essence, DateTimeFormatter offers a structure() method to accomplish the job.

Simply stated, DateTimeFormatter requires a time zone to format an instant. If it doesn’t have it, it’ll not convert the instant into human-readable date/time fields.

Let’s say, for instance, we’d like to show our Instant instance in an dd.MM.yyyy format:

public class FormatInstantUnitTest {

private static final String PATTERN_FORMAT = “dd.MM.yyyy”;

@Test
public void givenInstant_whenUsingDateTimeFormatter_thenFormat() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(PATTERN_FORMAT)
.withZone(ZoneId.systemDefault());

Instant instant = Instant.parse(“2022-02-15T18:35:24.00Z”);
String formattedInstant = formatter.format(instant);

assertThat(formattedInstant).isEqualTo(“15.02.2022”);
}

}

As we have seen in the previous paragraph, we can employ this withZone() method to define the time zone.

Please keep in mind that not specifying a time zone will result in an UnsupportedTemporalTypeException:

@Test(expected = UnsupportedTemporalTypeException.class)
public void givenInstant_whenNotSpecifyingTimeZone_thenThrowException() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(PATTERN_FORMAT);

Instant instant = Instant.now();
formatter.format(instant);
}

B) Utilizing the toString() Method

Another option is to use method toString() technique to obtain the representation in string format that is your Instant object.

Let’s illustrate the usage for the toString() method by using an example:

@Test
public void givenInstant_whenUsingToString_thenFormat() {
Instant instant = Instant.ofEpochMilli(1641828224000L);
String formattedInstant = instant.toString();

assertThat(formattedInstant).isEqualTo(“2022-01-10T15:23:44Z”);
}

The drawback of this method is that it doesn’t allow us to use an appropriate, human-friendly way to present the current.

3. Joda-Time Library

Another option is to use an alternative method, which is the Joda-Time API to achieve the same goal. The library offers a collection of pre-built classes and interfaces for manipulating time and date in Java.

In the list of classes, we have among them the DateTimeFormat class. The name itself suggests that this class is able to format or convert data on date/time to and from the form of a string.

Let’s look at how you can use DateTimeFormatter to convert an instant into string:

@Test
public void givenInstant_whenUsingJodaTime_thenFormat() {
org.joda.time.Instant instant = new org.joda.time.Instant(“2022-03-20T10:11:12”);

String formattedInstant = DateTimeFormat.forPattern(PATTERN_FORMAT)
.print(instant);

assertThat(formattedInstant).isEqualTo(“20.03.2022”);
}

4. Conclusion

In the end In this article, we went over in-depth the steps to format an instant as the form of a string using Java.

We examined a variety of methods to achieve this using standard Java methods. We then discussed how to do similar results using the Joda Time library.

SevenMentor Academy offers the best Software Testing, Full Stack, and Java course in Pune.

Enroll today at SevenMentor!

Leave a Reply

Your email address will not be published.