Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

1. Overview

Timestamp is one of a few legacy date-time objects in Java.

In this tutorial, we'll see how to parse from a String value to a Timestamp object and how to format a Timestamp object to a String.

And since Timestamp relies on a Java-proprietary format, we'll see how to effectively adapt.

2. Parse a String to a Timestamp

2.1. Standard Format

The simplest way to parse a String to a Timestamp is its valueOf method:

Timestamp.valueOf("2018-11-12 01:02:03.123456789")

And when our String is in JDBC timestamp format – yyyy-m[m]-d[d] hh:mm:ss[.f…] – then it's pretty simple.

We can interpret that pattern like this:

Pattern Description Example
yyyy Represents the year, and it's mandatory to have four digits for it. 2018
m[m] For the month component, we must have either one or two digits (from 1 to 12). 1, 11
d[d] For the day of month value, we must have either one or two digits (from 1 to 31). 7, 12
hh Stands for the hour of the day, with allowed values from 0 to 23. 01, 16
mm Stands for minutes of the hour, with allowed values from 0 to 59. 02, 45
ss Stands for seconds in the minute, with allowed values from 0 to 59. 03, 52
[.f…] Represents optional fractions of a second, can be up to nanoseconds precision so the allowed values are from 0 to 999999999. 12, 1567, 123456789

2.2. Alternative Formats

Now, if it isn't in JDBC timestamp format, then luckily, valueOf also takes a LocalDateTime instance.

This means we can take a date in any format, we just need to first convert it into a LocalDateTime:

String pattern = "MMM dd, yyyy HH:mm:ss.SSSSSSSS";
String timestampAsString = "Nov 12, 2018 13:02:56.12345678";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
LocalDateTime localDateTime = LocalDateTime.from(formatter.parse(timestampAsString));

And then we can use valueOf  we did before:

Timestamp timestamp = Timestamp.valueOf(localDateTime);
assertEquals("2018-11-12 13:02:56.12345678", timestamp.toString());

Note in passing that, unlike a Date object, a Timestamp object is capable of storing fractions of a second.

3. Format a Timestamp as a String

To format a Timestamp, we'll have the same challenge since it's default format is the proprietary JDBC timestamp format:

assertEquals("2018-11-12 13:02:56.12345678", timestamp.toString());

But, again, using an intermediary conversion, we can format the resulting String to a different date and time pattern, like the ISO-8601 standard:

Timestamp timestamp = Timestamp.valueOf("2018-12-12 01:02:03.123456789");
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
String timestampAsString = formatter.format(timestamp.toLocalDateTime());
assertEquals("2018-12-12T01:02:03.123456789", timestampAsString);

4. Conclusion

In this article, we saw how to convert between String and Timestamp objects in Java. Also, we saw how to use LocalDateTime conversion as an intermediary step in order to convert to, and from, different date and time patterns.

And make sure to find all of these examples and snippets over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


Generic footer banner
Inline Feedbacks
View all comments
Comments are closed on this article!