Partner – DBSchema – NPI (tag = SQL)
announcement - icon

DbSchema is a super-flexible database designer, which can take you from designing the DB with your team all the way to safely deploying the schema.

The way it does all of that is by using a design model, a database-independent image of the schema, which can be shared in a team using GIT and compared or deployed on to any database.

And, of course, it can be heavily visual, allowing you to interact with the database using diagrams, visually compose queries, explore the data, generate random data, import data or build HTML5 database reports.

>> Take a look at DBSchema

Course – LS – All

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

>> CHECK OUT THE COURSE

1. Overview

Both java.time.Instant and java.sql.Timestamp classes represent a point on the timeline in UTC. In other words, they represent the number of nanoseconds since the Java epoch.

In this quick tutorial, we’ll convert one to the other by using built-in Java methods.

2. Converting Instant to Timestamp and Back

We can use Timestamp.from() to convert Instants into Timestamps:

Instant instant = Instant.now();
Timestamp timestamp = Timestamp.from(instant);
assertEquals(instant.toEpochMilli(), timestamp.getTime());

And vice-versa, we can use Timestamp.toInstant() to convert Timestamps into Instants:

instant = timestamp.toInstant();
assertEquals(instant.toEpochMilli(), timestamp.getTime());

Either way, both the Instant and Timestamp represents the same point on the timeline.

Next, let’s have a look at the interaction between the two classes and the timezone.

3. toString() Method Differences

Invoking toString() on Instant and Timestamp behaves differently with respect to timezone. Instant.toString() returns the time in UTC timezone. On the other hand, Timezone.toString() returns the time in the local machine timezone.

Let’s see what we get when calling toString() on instant and timestamp respectively:

Instant (in UTC): 2018-10-18T00:00:57.907Z
Timestamp (in GMT +05:30): 2018-10-18 05:30:57.907

Here, timestamp.toString() resulted in a time which is 5 hours 30 minutes after the time returned by the instant.toString(). This is because the local machine’s timezone is at GMT +5:30 timezone.

The output of the toString() method is different, but both the timestamp and instant represent the same point on the timeline.

We can also verify this by converting the Timestamp to the UTC time zone:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
formatter = formatter.withZone(TimeZone.getTimeZone("UTC").toZoneId());
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

assertThat(formatter.format(instant)).isEqualTo(df.format(timestamp));

4. Conclusion

In this quick tutorial, we saw how to convert between java.time.Instant and java.sql.Timestamp classes in Java using built-in methods.

We also had a look at how the time zone affects how the output changes.

And, as always, the complete code examples 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.